petezaman Posted October 19, 2009 Share Posted October 19, 2009 Hi all, I'm pretty new to php and MySql and have hit a problem. I am able to display data in a table using the following code: <?php $database="MYDATABASE"; mysql_connect ("localhost", "MYUSERNAME", "MYPASSWORD"); @mysql_select_db($database) or die( "Unable to select database"); $result = mysql_query( "SELECT field1, field2, field3 FROM staff WHERE `group` = 'group1' ORDER BY `role` " ) or die("SELECT Error: ".mysql_error()); $num_rows = mysql_num_rows($result); print "There are $num_rows records.<P>"; print "<table width=200 border=1>\n"; while ($get_info = mysql_fetch_row($result)){ print "<tr>\n"; foreach ($get_info as $field) print "\t<td><font face=arial size=1/>$field</font></td>\n"; print "</tr>\n"; } print "</table>\n"; ?> However, I would like to display the results using the following format: <div id="info"><!--this is a div contanier for info"--> <h1> field1 </h1> <img src="field2" class="floatLeft" /><p>field3</p> </div><!--close info div--> Obviously, if there was more than one row of results, it would repeat the above format for each row. Any help would be greatly appreciated Thanks Pete Quote Link to comment https://forums.phpfreaks.com/topic/178207-solved-displaying-data-from-mysql-a-different-way/ Share on other sites More sharing options...
JAY6390 Posted October 19, 2009 Share Posted October 19, 2009 <?php $database = "MYDATABASE"; mysql_connect("localhost", "MYUSERNAME", "MYPASSWORD"); @mysql_select_db($database) or die("Unable to select database"); $result = mysql_query("SELECT field1, field2, field3 FROM staff WHERE `group` = 'group1' ORDER BY `role` ") or die("SELECT Error: ". mysql_error()); $num_rows = mysql_num_rows($result); print "There are $num_rows records.<P>"; print "<table width=200 border=1>\n"; while ($get_info = mysql_fetch_row($result)) { print '<div id="info"><!--this is a div contanier for info"--> <h1>'.$get_info[0].'</h1> <img src="'.$get_info[0].'" class="floatLeft" /><p>'.$get_info[2].'</p> </div><!--close info div-->'; } ?> That should do it. Your div has the id of "info". If you want to have the div wrap each individually you should change from id to class otherwise your markup will be invalid You can also overcome this by putting the div outside of the while loop, creating one container for all of the rows of data Quote Link to comment https://forums.phpfreaks.com/topic/178207-solved-displaying-data-from-mysql-a-different-way/#findComment-939576 Share on other sites More sharing options...
cags Posted October 19, 2009 Share Posted October 19, 2009 Learn to use the [code tags, they are your friend. Sounds like you one something like this... while ($get_info = mysql_fetch_array($result)){ echo '<div id="info"><!--this is a div contanier for info"-->'; echo '<h1>' . $get_info[0] . '<h1>'; echo '<img src="' . $get_info[1] . '" class="floatLeft" /><p>' . $get_info[2] . '</p>'; echo '<div>'; } Just a quick note, semantically speaking a page should only ever have one <h1> tag. Edit: d'oh. Quote Link to comment https://forums.phpfreaks.com/topic/178207-solved-displaying-data-from-mysql-a-different-way/#findComment-939577 Share on other sites More sharing options...
JAY6390 Posted October 19, 2009 Share Posted October 19, 2009 Didn't think about the <h1> tag. Good point Cags Quote Link to comment https://forums.phpfreaks.com/topic/178207-solved-displaying-data-from-mysql-a-different-way/#findComment-939579 Share on other sites More sharing options...
cags Posted October 19, 2009 Share Posted October 19, 2009 Technically speaking I don't think you should have multiple div's with the same id either. Quote Link to comment https://forums.phpfreaks.com/topic/178207-solved-displaying-data-from-mysql-a-different-way/#findComment-939582 Share on other sites More sharing options...
JAY6390 Posted October 19, 2009 Share Posted October 19, 2009 ^That's why I said to change it to a class instead of an id or move it outside of the while loop to only create one div wrapper Quote Link to comment https://forums.phpfreaks.com/topic/178207-solved-displaying-data-from-mysql-a-different-way/#findComment-939584 Share on other sites More sharing options...
petezaman Posted October 19, 2009 Author Share Posted October 19, 2009 Thanks Jay and Cags, I've changed the div to a class and it works perfectly. You guys are stars. Now I can try and figure out how to store the image link in the database after an image has been uploaded. Thanks again Pete Quote Link to comment https://forums.phpfreaks.com/topic/178207-solved-displaying-data-from-mysql-a-different-way/#findComment-939606 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.