0

I have a php script that will loop until all the skill, exp and rating variables are displayed that the user entered into the database.

What I want is the following code to only display once and only if the variables are holding info and not empty. How can I do this. I hope I explained it okay.

I want this code displayed first.

echo '<div id="con">';
echo '<h2 id="s">Skills</h2>';
echo '<h2 id="exp">Exp</h2>';
echo '<h2 id="r">Rating</h2>';

Here is the full code.

<?php
$mysqli = new mysqli("localhost", "root", "", "sitename");
$dbc = mysqli_query($mysqli,"SELECT * FROM skills WHERE user_id='3'");

if (!$dbc) {
    print mysqli_error();
}

echo '<div id="con">';
echo '<h2 id="s">Skills</h2>';
echo '<h2 id="exp">Exp</h2>';
echo '<h2 id="r">Rating</h2>';

while ($row = mysqli_fetch_assoc($dbc)) {
    if (!empty($row['skill']) || !empty($row['exp']) || !empty($row['rating'])) { 
        if (! empty($row['skill'])) {
                echo '<div class="s">';
                echo '<p>' , htmlspecialchars($row['skill']) , '</p>';
            }

        if (! empty($row['exp'])) {
                echo '<div class="s">';
                echo '<p>' , htmlspecialchars($row['exp']) , '</p>';
            }

        if (! empty($row['rating'])) {
                echo '<div class="s">';
                echo '<p>' , htmlspecialchars($row['rating']) , '</p>';
            }

    }
}

echo '</div>';
?>
2
  • 1
    Is the for play or for work? If for play/learning, Sab's answer is adequate. For work then you should look at a more structure framework which breaks out data access from the display. Commented Oct 21, 2009 at 17:34
  • i missed the last closing div .. i have placed it back now , where as the inside divs .. they dont have closing tag even in ur own code .. so if u correct that .. it should work aright and i agree with ChronoFish above totally. Commented Oct 21, 2009 at 19:46

1 Answer 1

3

you can take the following after the loop:

echo '<div id="con">';
echo '<h2 id="s">Skills</h2>';
echo '<h2 id="exp">Exp</h2>';
echo '<h2 id="r">Rating</h2>';

and instead of echo in the loops , use vars to store the text and then check if the var holds something and then echo

update: would become something like this:

$skills = "";
$exps = "";
$ratings = "";
while ($row = mysqli_fetch_assoc($dbc)) {
        if (!empty($row['skill']) || !empty($row['exp']) || !empty($row['rating'])) { 
                if (! empty($row['skill'])) {
                                $skills .='<div class="s">';
                                $skills .= '<p>' , htmlspecialchars($row['skill']) , '</p>';
                        }

                if (! empty($row['exp'])) {
                                $exps .= '<div class="s">';
                                $exps .= '<p>' , htmlspecialchars($row['exp']) , '</p>';
                        }

                if (! empty($row['rating'])) {
                                $ratings .= '<div class="s">';
                                $ratings .= '<p>' , htmlspecialchars($row['rating']) , '</p>';
                        }

        }
}

echo '<div id="con">';
if($skills){
 echo '<h2 id="s">Skills</h2>';
 echo $skills;
}

if($exps){
  echo '<h2 id="exp">Exp</h2>';
  echo $exps;
}

if($ratings){
  echo '<h2 id="r">Rating</h2>';
  echo $ratings;
}
echo '</div>';
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.