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>

Link to comment
Share on other sites

<?= ?> 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.

Link to comment
Share on other sites

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>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.