Waaaaat
Waaaaat

Reputation: 644

Display data from database depending on the how many I have entered

In my database and more specific in the table named portfolio a user with a speficic username can store more than one data. The columns of the database are id,username,portfolio and portfolio_description

enter image description here

and I want to echo in my web page all the data for this username for example username=nbourlai. Until now I can echo only the first row for each username what is the loop that i can use in order to echo as many time is the id number. Below is the current result and i want to create Portfolio and Portfolio description to be displayed with the appropriate values as many time as the highest id number is for the specific user.

enter image description here

Here is my code...

$portfolio = queryMysql("SELECT portfolio,portfolio_description FROM portfolio WHERE username='nbourlai'");

$row = mysql_fetch_row($portfolio);
echo "<h2>Portfolio</h2>";
echo "Portfolio: ";
echo stripslashes($row[0]) . "<br/>Portfolio Description: ";
echo stripslashes($row[1]) . "<br clear=left /><br />";

Can you help me please?

Upvotes: 0

Views: 50

Answers (1)

Sithu
Sithu

Reputation: 4862

Are you looking for while loop like this?

$portfolio = queryMysql("SELECT portfolio,portfolio_description FROM portfolio WHERE username='nbourlai'");
echo "<h2>Portfolio</h2>";
while($row = mysql_fetch_row($portfolio)){
    echo "Portfolio: ";
    echo stripslashes($row[0]) . "<br/>Portfolio Description: ";
    echo stripslashes($row[1]) . "<br clear=left /><br />";
}

or, like this?

$portfolio = queryMysql("SELECT username,portfolio,portfolio_description FROM portfolio ORDER BY username");
$username = '';
while($row = mysql_fetch_row($portfolio)){
    if($username != $row[0]){
        echo "<h1>".$row[0]."</h1>";
        echo "<h2>Portfolio</h2>";
        echo "Portfolio: ";
        echo stripslashes($row[1]) . "<br/>Portfolio Description: ";
        echo stripslashes($row[2]) . "<br clear=left /><br />";
    }else{
        echo "Portfolio: ";
        echo stripslashes($row[1]) . "<br/>Portfolio Description: ";
        echo stripslashes($row[2]) . "<br clear=left /><br />";         
    }
    $username = $row[0];
}

Upvotes: 1

Related Questions