Jump to content

ignore field if no data? PHP and CSV file


lauradesigner

Recommended Posts

I want half of the results to to show up with a hyperlink (if I've added the link as a field in my CSV file) and half of the results to show up with no hyperlink. The link URL's are located in field 8 of a database : [8] as seen below.

 

Could anyone help me to figure out how to add an "if" statement perhaps? Something like "if 8 true" then display "<a href"?

 

I did not write this php code I am only modifying it. The csv file is very very straightforward, but this PHP is beyond me. Would appreciate the help!

 

<p><a href="<?=htmlspecialchars($r[8])?>"><?=htmlspecialchars($r[1])?></a><br />

        <?=htmlspecialchars($r[2])?><br />

        <?=htmlspecialchars($r[3])?><br />

        <?=htmlspecialchars($r[4])?><br />

        <?=htmlspecialchars($r[5])?><br />

        <a href="mailto:<?=htmlspecialchars($r[7])?>"><?=htmlspecialchars($r[7])?></a>

    </p>

<?= ?> is the same as <?php echo ?> so <?=htmlspecialchars($r[2])?> is just <?php echo htmlspecialchars($r[2]); ?>

 

so if field 8 has a link put in a link, if not dont add the link?

<p><?=(!empty(htmlspecialchars($r[8]))) ? "<a href=\"" . htmlspecialchars($r[8]) . "\">" . htmlspecialchars($r[1]) . "</a>" : htmlspecialchars($r[1])?><br />
        <?=htmlspecialchars($r[2])?><br />
        <?=htmlspecialchars($r[3])?><br />
        <?=htmlspecialchars($r[4])?><br />
        <?=htmlspecialchars($r[5])?><br />
        <a href="mailto:<?=htmlspecialchars($r[7])?>"><?=htmlspecialchars($r[7])?></a>
</p>


That says if field 8 is not empty output field 1 with the link on it else just output field 1. Let me know if that's not what you meant.

I put it in but it doesn't work. This is the error:

 

Fatal error: Can't use function return value in write context in /home/edgecre/www/www/contact.php on line 21

 

Thanks for the quick reply. Let me know if you have other ideas or a variation on this one.

bah. try this. syntax error.

<p><?=(!empty($r[8])) ? "<a href=\"" . htmlspecialchars($r[8]) . "\">" . htmlspecialchars($r[1]) . "</a>" : htmlspecialchars($r[1])?><br />
        <?=htmlspecialchars($r[2])?><br />
        <?=htmlspecialchars($r[3])?><br />
        <?=htmlspecialchars($r[4])?><br />
        <?=htmlspecialchars($r[5])?><br />
        <a href="mailto:<?=htmlspecialchars($r[7])?>"><?=htmlspecialchars($r[7])?></a>
</p>

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.