Board index   FAQ   Search  
Register  Login
Board index php forum :: Database mySQL & php coding

populating php table from dabase

Codes here !

Moderators: macek, egami, gesf

populating php table from dabase

Postby elvisdominguez » Wed May 16, 2012 8:13 am

hi, am newbie in php,am trying to populate a html table from a database;each row must contain only 4 cells filled with the categories name. Bellow is the code that i have created but it shows repeated categories names in each row, can somebody help me plz!

<?php

/**
* use case 1:Select a Category
* @author elvis
* @copyright 2012
*/

require_once ('system/config/config.php');

$connect = mysql_connect(DB_HOSTNAME,DB_USERNAME,DB_PASSWORD) or die ("check your server connection.");
mysql_select_db(DB_DATABASE);
$quey1="select * from category";
$result=mysql_query($quey1) or die(mysql_error());

?>
<div align="center">
<table border="1" width="600">
<caption><EM>Select a Category</EM></caption>

<?php
while($row=mysql_fetch_array($result)){ ?>
<tr>

<?php $catid=$row['category_id'];?>
<td align='center'>
<img src="<?php echo $row['category_image']; ?>" /><br />
<a href="http://localhost/GoDeli/model/bo/select.php?catid=<?php echo $catid ?>"><?php echo $row['category_name']; ?></a>

</td>
<td align='center'>
<img src="<?php echo $row['category_image']; ?>" /><br />
<a href="http://localhost/GoDeli/model/bo/select.php?catid=<?php echo $catid ?>"><?php echo $row['category_name']; ?></a>

</td>
</tr>
<?php } ?>
</table>


</div>
elvisdominguez
New php-forum User
New php-forum User
 
Posts: 3
Joined: Wed May 16, 2012 8:02 am

Re: populating php table from dabase

Postby elvisdominguez » Wed May 16, 2012 12:56 pm

Got it, After thousands of searches on the web(ofcourse Google) i found the answer, here's the working code for any newbies out there, thanks!



<div align="center">
<?php
require_once ('system/config/config.php');

$connect = mysql_connect(DB_HOSTNAME,DB_USERNAME,DB_PASSWORD) or die ("check your server connection.");
mysql_select_db(DB_DATABASE);
$quey1="select * from category";
$result=mysql_query($quey1) or die(mysql_error());

?>


<table border="1" width="600">
<caption><EM>Select a Category</EM></caption>
<?php
$records = array();
for ($i = 1; $record = mysql_fetch_array($result); $i++)
$records[$i] = $record;
?>
<?php foreach (array_chunk($records, 4) as $row) { ?>
<tr>
<?php foreach ($row as $value) { ?>
<td height="150" align="center"><?php $catid=$value['category_id'];?><img src="<?php echo $value['category_image']; ?>" /><br /><a href="http://localhost/GoDeli/model/bo/select.php?catid=<?php echo $catid ?>"><?php echo $value['category_name']; ?></a></td>
<?php } ?>
</tr>
<?php } ?>
</table>

</div>
elvisdominguez
New php-forum User
New php-forum User
 
Posts: 3
Joined: Wed May 16, 2012 8:02 am

Re: populating php table from dabase

Postby elvisdominguez » Thu May 17, 2012 7:52 am

Thanks for the suggestion!
elvisdominguez
New php-forum User
New php-forum User
 
Posts: 3
Joined: Wed May 16, 2012 8:02 am


Return to mySQL & php coding

Who is online

Users browsing this forum: Bing [Bot] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.