0

I have this code

$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("juliver", $con);

$result = mysql_query("SELECT * FROM menu");

$sm = "";
while($row = mysql_fetch_array($result))
{

$sm .= "<li><a href='#".$row['page'].'">'.$row['menulist']."</a></li>";

}

mysql_close($con);

?>
<? echo $sm; ?>

My database is look like this.

id    menulist          page
1     Home              tb1
2     Gallery           tb2
3     Clothing          tb3
4     Furniture         tb4
5     Household-items   tb5

the output should be this if converted into html.

<li><a href="#tb1">Home</a></li>
<li><a href="#tb2">Gallery</a></li>
<li><a href="#tb3">Clothing</a></li>
<li><a href="#tb4">Furniture</a></li>
<li><a href="#tb5">Household-items</a></li>

I tried removing, adding, renaming and etc into the code and etc. but im still stuck and none of them work.

please help me.

3
  • Are you sure you are getting results? Check mysql_num_rows() Commented Mar 29, 2012 at 13:56
  • maybe, you cannot select db. try this. mysql_select_db("juliver", $con) or die("cannont select: ".nysql_error()); Commented Mar 29, 2012 at 13:59
  • You need UL as parent of LIs Commented Mar 29, 2012 at 14:07

2 Answers 2

1

Your quotes seem to be muddled up. Your literal text should be enclosed in double quotes (since they appear first), and everything else use single quotes:

$sm .= "<li><a href='#".$row['page']."'>".$row['menulist']."</a></li>";

results in

$sm .= "<li><a href='#tb1'>Home</a></li>";

which you can then output.

You might want to add a \n in there as well, to format the output nicely.

$sm .= "<li><a href='#".$row['page']."'>".$row['menulist']."</a></li>\n";

Sign up to request clarification or add additional context in comments.

Comments

0

I think you may be not open the php_short_tags!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.