[PHP] verschillende tabellen bij if / else

Pagina: 1
Acties:

Vraag


Acties:
  • 0 Henk 'm!

  • ImNotnoa
  • Registratie: September 2011
  • Niet online
Hallo allen,

Allereerst: Ik ben geen programmeur van beroep, dus het kan zijn dat een echte programmeur verschrikkelijke uitslag krijgt van mijn code, bij voorbaat mijn excuses.

Mijn vraag:

Onze webshop bevat een opmerkingen veld ($comment), dit veld wordt ook meegestuurd met de bevestiging van de bestelling per email

Ik wil graag dat als in dit veld de string "Scan app order" wordt gevuld in een andere tabel weergeven dan als deze niet wordt gevuld (in de emailbevestiging).

Daarvoor heb ik onderstaande code bij elkaar gegoogled, ik heb al vele variaties geprobeerd en heb het gevoel dat deze zou moeten werken, maar dat doet hij helaas niet.


PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  <?php
if ( strpos( $comment, 'Scan app order' ) !== false  { ?>
  <?php  echo '<tr>
        <td style="font-size: 12px; border-right: 1px solid #DDDDDD; border-bottom: 1px solid #DDDDDD; background-color: #EFEFEF; font-weight: bold; text-align: left; padding: 7px; color: #222222;"><?php echo $text_product; ?></td>
        <td style="font-size: 12px; border-right: 1px solid #DDDDDD; border-bottom: 1px solid #DDDDDD; background-color: #EFEFEF; font-weight: bold; text-align: left; padding: 7px; color: #222222;"><?php echo $text_model; ?></td>
        <td style="font-size: 12px; border-right: 1px solid #DDDDDD; border-bottom: 1px solid #DDDDDD; background-color: #EFEFEF; font-weight: bold; text-align: right; padding: 7px; color: #222222;"><?php echo $text_quantity; ?></td>
     </tr> ?>'; ?>
<?php } else: { ?>
 <?php   echo '<tr>
        <td style="font-size: 12px; border-right: 1px solid #DDDDDD; border-bottom: 1px solid #DDDDDD; background-color: #EFEFEF; font-weight: bold; text-align: left; padding: 7px; color: #222222;"><?php echo $text_product; ?></td>
        <td style="font-size: 12px; border-right: 1px solid #DDDDDD; border-bottom: 1px solid #DDDDDD; background-color: #EFEFEF; font-weight: bold; text-align: left; padding: 7px; color: #222222;"><?php echo $text_model; ?></td>
        <td style="font-size: 12px; border-right: 1px solid #DDDDDD; border-bottom: 1px solid #DDDDDD; background-color: #EFEFEF; font-weight: bold; text-align: right; padding: 7px; color: #222222;"><?php echo $text_quantity; ?></td>
        <td style="font-size: 12px; border-right: 1px solid #DDDDDD; border-bottom: 1px solid #DDDDDD; background-color: #EFEFEF; font-weight: bold; text-align: right; padding: 7px; color: #222222;"><?php echo $text_price; ?></td>
        <td style="font-size: 12px; border-right: 1px solid #DDDDDD; border-bottom: 1px solid #DDDDDD; background-color: #EFEFEF; font-weight: bold; text-align: right; padding: 7px; color: #222222;"><?php echo $text_total; ?></td>
     </tr>'; ?>
}
<?php ?>
  


De webserver draait op IIS10 met PHP 7.1

De variabele $comment wordt eerder in het script ook al gebruikt, kan dit ervoor zorgen dat mijn code niet werkt?

PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
 <?php if ($comment) { ?>
  <table style="border-collapse: collapse; width: 100%; border-top: 1px solid #DDDDDD; border-left: 1px solid #DDDDDD; margin-bottom: 20px;">
    <thead>
      <tr>
        <td style="font-size: 12px; border-right: 1px solid #DDDDDD; border-bottom: 1px solid #DDDDDD; background-color: #EFEFEF; font-weight: bold; text-align: left; padding: 7px; color: #222222;"><?php echo $text_instruction; ?></td>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td style="font-size: 12px; border-right: 1px solid #DDDDDD; border-bottom: 1px solid #DDDDDD; text-align: left; padding: 7px;"><?php echo $comment; ?></td>
      </tr>
    </tbody>
  </table>
  <?php } ?>


Als jullie mij een beetje op weg kunnen helpen zou dit top zijn!

Alvast bedankt!

Try SCE to Aux

Beste antwoord (via ImNotnoa op 10-10-2019 09:28)


  • Solaire
  • Registratie: December 2014
  • Laatst online: 30-09 14:04
Ik heb hem even opgeschoond voor je. Dit zou moeten werken?

PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
if ( strpos( $comment, 'Scan app order' ) !== false)  {
  echo '<tr>
        <td style="font-size: 12px; border-right: 1px solid #DDDDDD; border-bottom: 1px solid #DDDDDD; background-color: #EFEFEF; font-weight: bold; text-align: left; padding: 7px; color: #222222;">' . $text_product . '</td>
        <td style="font-size: 12px; border-right: 1px solid #DDDDDD; border-bottom: 1px solid #DDDDDD; background-color: #EFEFEF; font-weight: bold; text-align: left; padding: 7px; color: #222222;">' . $text_model . '</td>
        <td style="font-size: 12px; border-right: 1px solid #DDDDDD; border-bottom: 1px solid #DDDDDD; background-color: #EFEFEF; font-weight: bold; text-align: right; padding: 7px; color: #222222;">' . $text_quantity . '</td>
        <td></td>
        <td></td>
     </tr>';
} else {
    echo '<tr>
        <td style="font-size: 12px; border-right: 1px solid #DDDDDD; border-bottom: 1px solid #DDDDDD; background-color: #EFEFEF; font-weight: bold; text-align: left; padding: 7px; color: #222222;">' .  $text_product . '</td>
        <td style="font-size: 12px; border-right: 1px solid #DDDDDD; border-bottom: 1px solid #DDDDDD; background-color: #EFEFEF; font-weight: bold; text-align: left; padding: 7px; color: #222222;">' . $text_model . '</td>
        <td style="font-size: 12px; border-right: 1px solid #DDDDDD; border-bottom: 1px solid #DDDDDD; background-color: #EFEFEF; font-weight: bold; text-align: right; padding: 7px; color: #222222;">' .  $text_quantity . '</td>
        <td style="font-size: 12px; border-right: 1px solid #DDDDDD; border-bottom: 1px solid #DDDDDD; background-color: #EFEFEF; font-weight: bold; text-align: right; padding: 7px; color: #222222;">' .  $text_price . '</td>
        <td style="font-size: 12px; border-right: 1px solid #DDDDDD; border-bottom: 1px solid #DDDDDD; background-color: #EFEFEF; font-weight: bold; text-align: right; padding: 7px; color: #222222;">' .  $text_total . '</td>
     </tr>';
}
?>

Alle reacties


Acties:
  • +1 Henk 'm!

  • Jivebunny
  • Registratie: November 2010
  • Niet online

Jivebunny

Fail to plan. Plan to fail.

Er zit een fout in je code:

syntax error, unexpected '{' [2,1]

Misschien een probleem in mijn linter overigens.

[ Voor 28% gewijzigd door Jivebunny op 10-10-2019 09:11 ]

Pixel 9 Pro XL | NAS: HP Gen8 e3-1265L v2 16GB 12TB unRAID 6.9.2 | D: Ryzen 5600x 16GB 3000mhz RX 6800 XT MB Acer 27" IPS 144hz | Suzuki Swift 1.2 (2013)


Acties:
  • Beste antwoord
  • +2 Henk 'm!

  • Solaire
  • Registratie: December 2014
  • Laatst online: 30-09 14:04
Ik heb hem even opgeschoond voor je. Dit zou moeten werken?

PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
if ( strpos( $comment, 'Scan app order' ) !== false)  {
  echo '<tr>
        <td style="font-size: 12px; border-right: 1px solid #DDDDDD; border-bottom: 1px solid #DDDDDD; background-color: #EFEFEF; font-weight: bold; text-align: left; padding: 7px; color: #222222;">' . $text_product . '</td>
        <td style="font-size: 12px; border-right: 1px solid #DDDDDD; border-bottom: 1px solid #DDDDDD; background-color: #EFEFEF; font-weight: bold; text-align: left; padding: 7px; color: #222222;">' . $text_model . '</td>
        <td style="font-size: 12px; border-right: 1px solid #DDDDDD; border-bottom: 1px solid #DDDDDD; background-color: #EFEFEF; font-weight: bold; text-align: right; padding: 7px; color: #222222;">' . $text_quantity . '</td>
        <td></td>
        <td></td>
     </tr>';
} else {
    echo '<tr>
        <td style="font-size: 12px; border-right: 1px solid #DDDDDD; border-bottom: 1px solid #DDDDDD; background-color: #EFEFEF; font-weight: bold; text-align: left; padding: 7px; color: #222222;">' .  $text_product . '</td>
        <td style="font-size: 12px; border-right: 1px solid #DDDDDD; border-bottom: 1px solid #DDDDDD; background-color: #EFEFEF; font-weight: bold; text-align: left; padding: 7px; color: #222222;">' . $text_model . '</td>
        <td style="font-size: 12px; border-right: 1px solid #DDDDDD; border-bottom: 1px solid #DDDDDD; background-color: #EFEFEF; font-weight: bold; text-align: right; padding: 7px; color: #222222;">' .  $text_quantity . '</td>
        <td style="font-size: 12px; border-right: 1px solid #DDDDDD; border-bottom: 1px solid #DDDDDD; background-color: #EFEFEF; font-weight: bold; text-align: right; padding: 7px; color: #222222;">' .  $text_price . '</td>
        <td style="font-size: 12px; border-right: 1px solid #DDDDDD; border-bottom: 1px solid #DDDDDD; background-color: #EFEFEF; font-weight: bold; text-align: right; padding: 7px; color: #222222;">' .  $text_total . '</td>
     </tr>';
}
?>

Acties:
  • 0 Henk 'm!

  • ImNotnoa
  • Registratie: September 2011
  • Niet online
Jivebunny schreef op donderdag 10 oktober 2019 @ 09:02:
Er zit een fout in je code:

syntax error, unexpected '{' [2,1]

Misschien een probleem in mijn linter overigens.
Ik zag hem ook, ga er even mee aan de slag
Solaire schreef op donderdag 10 oktober 2019 @ 09:11:
Ik heb hem even opgeschoond voor je. Dit zou moeten werken?
Ik ga hem meteen testen!

Ik zag overal dat html in een echo binnen <php ?> werd geplaatst, dat hoeft dus niet?

[ Voor 34% gewijzigd door ImNotnoa op 10-10-2019 09:14 ]

Try SCE to Aux


Acties:
  • +2 Henk 'm!

  • Solaire
  • Registratie: December 2014
  • Laatst online: 30-09 14:04
Imnoa schreef op donderdag 10 oktober 2019 @ 09:12:
[...]


Ik zag hem ook, ga er even mee aan de slag
Je mist een ")" achter "false" :) . Heb hem in mijn code al aangepast en heb ook even twee lege "td" elementen toegevoegd zodat je tabel er niet raar uit komt te zien.

-edit-
Imnoa schreef op donderdag 10 oktober 2019 @ 09:12:
Ik zag overal dat html in een echo binnen <php ?> werd geplaatst, dat hoeft dus niet?
Het kan, maar je kunt beter string concatenation gebruiken. Zie ook https://www.php.net/manual/en/language.operators.string.php

Omdat sommige van je <?php tags binnen een al geopende PHP tag stonden kreeg je simpelweg die PHP tags in je HTML output te zien wat niet wenselijk is dunkt me :P

[ Voor 44% gewijzigd door Solaire op 10-10-2019 09:17 ]


Acties:
  • +2 Henk 'm!

  • Jivebunny
  • Registratie: November 2010
  • Niet online

Jivebunny

Fail to plan. Plan to fail.

Hier minified:

[ Voor 97% gewijzigd door Jivebunny op 10-10-2019 09:26 ]

Pixel 9 Pro XL | NAS: HP Gen8 e3-1265L v2 16GB 12TB unRAID 6.9.2 | D: Ryzen 5600x 16GB 3000mhz RX 6800 XT MB Acer 27" IPS 144hz | Suzuki Swift 1.2 (2013)


Acties:
  • +2 Henk 'm!

  • Solaire
  • Registratie: December 2014
  • Laatst online: 30-09 14:04
Jivebunny schreef op donderdag 10 oktober 2019 @ 09:18:
Hier minified:

code:
1
2
3
<?php if(strpos($comment,'Scan app order')!==false){ ?><?php echo '<tr>
        <td style="font-size: 12px; border-right: 1px solid #DDDDDD; border-bottom: 1px solid #DDDDDD; background-color: #EFEFEF; font-weight: bold; text-align: left; padding: 7px; color: #222222;"><?php echo $text_product; ?><td style="font-size:12px;border-right:1px solid #ddd;border-bottom:1px solid #ddd;background-color:#efefef;font-weight:700;text-align:left;padding:7px;color:#222"><?php echo $text_model; ?></td><td style="font-size:12px;border-right:1px solid #ddd;border-bottom:1px solid #ddd;background-color:#efefef;font-weight:700;text-align:right;padding:7px;color:#222"><?php echo $text_quantity; ?></td>?>'; ?><?php }else:{ ?><?php echo '<tr>
        <td style="font-size: 12px; border-right: 1px solid #DDDDDD; border-bottom: 1px solid #DDDDDD; background-color: #EFEFEF; font-weight: bold; text-align: left; padding: 7px; color: #222222;"><?php echo $text_product; ?><td style="font-size:12px;border-right:1px solid #ddd;border-bottom:1px solid #ddd;background-color:#efefef;font-weight:700;text-align:left;padding:7px;color:#222"><?php echo $text_model; ?></td><td style="font-size:12px;border-right:1px solid #ddd;border-bottom:1px solid #ddd;background-color:#efefef;font-weight:700;text-align:right;padding:7px;color:#222"><?php echo $text_quantity; ?></td><td style="font-size:12px;border-right:1px solid #ddd;border-bottom:1px solid #ddd;background-color:#efefef;font-weight:700;text-align:right;padding:7px;color:#222"><?php echo $text_price; ?></td><td style="font-size:12px;border-right:1px solid #ddd;border-bottom:1px solid #ddd;background-color:#efefef;font-weight:700;text-align:right;padding:7px;color:#222"><?php echo $text_total; ?></td>'; ?> }<?php  ?>
Een van de problemen die OP heeft is dat hij PHP tags binnen PHP tags heeft. Jouw minified variant heeft dit ook. Botweg gezegd heeft hij dit:

PHP:
1
2
3
<?php
echo '<?php echo $text_model; ?>';
?>


En dat werkt niet zoals OP het bedoelt :)

Acties:
  • +2 Henk 'm!

  • Room42
  • Registratie: September 2001
  • Niet online
@Imnoa Jij loopt toch lang genoeg rond om te weten dat een "Hij doet het niet", zonder foutmelding nog de meeste jeuk geeft, toch! :D
Imnoa schreef op donderdag 10 oktober 2019 @ 09:12:
[...]

Ik zag overal dat html in een echo binnen <php ?> werd geplaatst, dat hoeft dus niet?
Wat jij in jouw code doet is steeds 'php' afsluiten om hem direct weer te openen. Dat voegt niks toe en werkt juist ongewenste/onverwachte white-space in de hand. :)

@Jivebunny Minifyen van code slaat imho echt nergens op. Hou het liever leesbaar. :o

"Technological advancements don't feel fun anymore because of the motivations behind so many of them." Bron


Acties:
  • 0 Henk 'm!

  • Jivebunny
  • Registratie: November 2010
  • Niet online

Jivebunny

Fail to plan. Plan to fail.

@Solaire True, verkeerde code gepakt.
@Room42 Ook waar. Obfuscaten in productie dan maar.

[ Voor 3% gewijzigd door Jivebunny op 10-10-2019 09:25 ]

Pixel 9 Pro XL | NAS: HP Gen8 e3-1265L v2 16GB 12TB unRAID 6.9.2 | D: Ryzen 5600x 16GB 3000mhz RX 6800 XT MB Acer 27" IPS 144hz | Suzuki Swift 1.2 (2013)


Acties:
  • 0 Henk 'm!

  • ImNotnoa
  • Registratie: September 2011
  • Niet online
Room42 schreef op donderdag 10 oktober 2019 @ 09:22:
@Imnoa Jij loopt toch lang genoeg rond om te weten dat een "Hij doet het niet", zonder foutmelding nog de meeste jeuk geeft, toch! :D
Hij doet het niet zonder foutmelding in de error logs :+ not kidding

Try SCE to Aux


Acties:
  • +1 Henk 'm!

  • Room42
  • Registratie: September 2001
  • Niet online
Jivebunny schreef op donderdag 10 oktober 2019 @ 09:23:
@Room42 Ook waar. Obfuscaten in productie dan maar.
De code verlaat de server niet, dus dat is nergens voor nodig. Met JavaScript heb je een punt maar dat is hier niet van toepassing.

"Technological advancements don't feel fun anymore because of the motivations behind so many of them." Bron


Acties:
  • +2 Henk 'm!

  • DataGhost
  • Registratie: Augustus 2003
  • Laatst online: 03-10 23:11

DataGhost

iPL dev

Jivebunny schreef op donderdag 10 oktober 2019 @ 09:18:
Hier minified:

code:
1
2
3
<?php if(strpos($comment,'Scan app order')!==false){ ?><?php echo '<tr>
        <td style="font-size: 12px; border-right: 1px solid #DDDDDD; border-bottom: 1px solid #DDDDDD; background-color: #EFEFEF; font-weight: bold; text-align: left; padding: 7px; color: #222222;"><?php echo $text_product; ?><td style="font-size:12px;border-right:1px solid #ddd;border-bottom:1px solid #ddd;background-color:#efefef;font-weight:700;text-align:left;padding:7px;color:#222"><?php echo $text_model; ?></td><td style="font-size:12px;border-right:1px solid #ddd;border-bottom:1px solid #ddd;background-color:#efefef;font-weight:700;text-align:right;padding:7px;color:#222"><?php echo $text_quantity; ?></td>?>'; ?><?php }else:{ ?><?php echo '<tr>
        <td style="font-size: 12px; border-right: 1px solid #DDDDDD; border-bottom: 1px solid #DDDDDD; background-color: #EFEFEF; font-weight: bold; text-align: left; padding: 7px; color: #222222;"><?php echo $text_product; ?><td style="font-size:12px;border-right:1px solid #ddd;border-bottom:1px solid #ddd;background-color:#efefef;font-weight:700;text-align:left;padding:7px;color:#222"><?php echo $text_model; ?></td><td style="font-size:12px;border-right:1px solid #ddd;border-bottom:1px solid #ddd;background-color:#efefef;font-weight:700;text-align:right;padding:7px;color:#222"><?php echo $text_quantity; ?></td><td style="font-size:12px;border-right:1px solid #ddd;border-bottom:1px solid #ddd;background-color:#efefef;font-weight:700;text-align:right;padding:7px;color:#222"><?php echo $text_price; ?></td><td style="font-size:12px;border-right:1px solid #ddd;border-bottom:1px solid #ddd;background-color:#efefef;font-weight:700;text-align:right;padding:7px;color:#222"><?php echo $text_total; ?></td>'; ?> }<?php  ?>
Wat heb je aan minified PHP-code, behalve dat je het voor jezelf onleesbaar maakt? Zodat je bijvoorbeeld makkelijk over het hoofd ziet dat je code niet werkt omdat je PHP-tags genest hebt?
edit:
Ik zie dat ik langzaam ben vandaag

Acties:
  • 0 Henk 'm!

  • Room42
  • Registratie: September 2001
  • Niet online
Imnoa schreef op donderdag 10 oktober 2019 @ 09:23:
[...]


Hij doet het niet zonder foutmelding in de error logs :+ not kidding
Waarschijnlijk moet je even error_reporting(E_ALL); plaatsen (binnen de PHP-tags).
PHP:
1
<?php error_reporting(E_ALL); ?>


@Imnoa Overigens, PHP op IIS heb ik geen verstand van. Misschien moet je ook je logging aanpassen zodat errors wél gelogd worden. Lijkt me in iedere geval wel verstandig. ;)

[ Voor 23% gewijzigd door Room42 op 10-10-2019 09:29 ]

"Technological advancements don't feel fun anymore because of the motivations behind so many of them." Bron


Acties:
  • +2 Henk 'm!

  • ImNotnoa
  • Registratie: September 2011
  • Niet online
Room42 schreef op donderdag 10 oktober 2019 @ 09:25:
[...]

Waarschijnlijk moet je even error_reporting(E_ALL); plaatsen (binnen de PHP-tags).
PHP:
1
<?php error_reporting(E_ALL); ?>
Dat had ik al, ook in de ini

Ondertussen werkt hij dankzij @Solaire en @Jivebunny en (waar het me vooral om ging) weet ik nu wat ik verkeerd deed

Bedankt allen!
Room42 schreef op donderdag 10 oktober 2019 @ 09:25:
[...]

@Imnoa Overigens, PHP op IIS heb ik geen verstand van. Misschien moet je ook je logging aanpassen zodat errors wél gelogd worden. Lijkt me in iedere geval wel verstandig. ;)
Oh geloof me, er worden errors gelogd :+ alleen op de een of andere manier kwam er met mijn code geen mail én geen error

[ Voor 29% gewijzigd door ImNotnoa op 10-10-2019 09:32 ]

Try SCE to Aux

Pagina: 1