Pierreszwk Pierreszwk - 2 months ago 5x
HTML Question

Check if element exist on my php BD and display it on the html

I start in the world of PHP and have a few problems lately.
I explain to you:

I have a database with multiple tables. One of my tables contain a list of information on how social networks ( facebook, twitter , youtube, instagram , etc ...) .
Some of these social networks are null and others contain the web address that points to the network in question.

Html I created a hard list will be displayed where social networks of people if it has . So I checked if the social network of the person is displayed and if it is in the database .

In summary, I want to know how I can only display links to social networks that do not have NULL

I do not know if this is clear . Feel free to ask me more details!

Thank you for your help

<!-- Social Network -->
<div class="small-12 column network ">
<ul class="social">
<li id="facebook">
<a href="#"> <img src="./img/social/icn_facebook.svg" alt="facebook" /> </a>
<li id="twitter">
<a href="#"> <img src="./img/social/icn_twitter.svg" alt="twitter" /> </a>
<li id="google">
<a href="#"> <img src="./img/social/icn_google.svg" alt="google" /></a>
<li id="insta">
<a href="#"> <img src="./img/social/icn_instagram.svg" alt="instagram" /> </a>
<li id="youtube">
<a href="#"> <img src="./img/social/icn_youtube.svg" alt="youtube" /> </a>

image of a my data base section


Fetch the data from your db in a variable e.g. $socialrows for instance: $socialrows = $db->query($query); Where $db is your variable for your db where you connection is setup. $query holds your select query. Loop through the rows you fetched (I'm assuming you fetch all at once). Then use if statement to check if facebook db field etc. is null. If you're only dealing with one record at a time you can drop the loop.

<ul class="social">
    <?php foreach ($socialrows as $socialrow): 
          <li id="facebook">
            <a href="<?php echo $socialrow['facebook']; ?>"> 
                        <img src="./img/social/icn_facebook.svg" alt="facebook" /> </a>
          </li><?php endif;
          <li id="twitter">
            <a href="<?php echo $socialrow['twitter']; ?>"> 
                        <img src="./img/social/icn_twitter.svg" alt="twitter" /> </a>
          </li><?php endif;
     endforeach; ?>

Instead of storing the social sites in individual db fields, you could have one field and store their social sites in an array and then use the switch statement to do it for all the social sites. http://www.w3schools.com/php/php_switch.asp Note: I'm going on the basis that you're just using php and html here, I didn't consider a framework or Javascript.