...

View Full Version : Dynamic table with bullets question



dk4210
01-30-2012, 01:59 PM
Hello Guys,

I have a script that creates a dynamic <tr> from the a db query. The issue I am having is that it creates more bullets than are needed. Here is my script



// First Query to grab Feature1
$result = mysql_query("SELECT * FROM features WHERE f_id IN ( $feature3 )
") or die("Sql error : " . mysql_error());

while($row = mysql_fetch_assoc ($result)){
// $f_name=$row["f_name"];
// echo "<br>".$f_name;
// -- This array will hold your data,
// -- I used it to hold a title, an id and an author
// -- but it can be extended based on your own needs
$items[] = array(1 => $row['f_name']);
}
// Default # of Columns
$numcols = 3;

// Number of Items
$numitems = count($items);

// Number of Rows
$numrows = ceil($numitems/$numcols);

echo '<table border="0" bgcolor="#f0eeee" width="97%">';
for ($row=1; $row <= $numrows; $row++)
{
$cell = 0;
echo ' <tr>'."\n";
for ($col=1; $col <= $numcols; $col++)
{
echo ' <td width="'.round(100/$numcols).'%">'."\n";

if ($col===1)
{
$cell += $row;
print "<div class=\"feaureitems\"><img src=\"images/squarebul.png\">&nbsp;".$items[$cell - 1][1]."</div>"; /* $items[$cell - 1][1] to display title $items[$cell - 1][2] etc... */
}
else {
$cell += $numrows;
print "<div class=\"feaureitems\"><img src=\"images/squarebul.png\">&nbsp;".$items[$cell - 1][1]."</div>";
}
echo ' </td>'."\n";
}
echo ' </tr>'."\n";
}
echo '</table>';


}
}



Here is the problem (See attachment)

What can I do to stop this from happening?

mlseim
01-30-2012, 02:26 PM
What is the purpose of the array called $items ?

You have a MySQL database ... that contains all the information you need.
When you query it, the result is already an array. I'm confused.

dk4210
01-30-2012, 02:32 PM
Hey thanks for the response.. I think it's because in the database the table holds values like 1,2,3,4,5

I am converting them to an array.. That part actually works well.. Its the additional <td>'s being created with the bullets is the problem.

mlseim
01-30-2012, 02:35 PM
if ($col===1)
{
$cell += $row;
if($items[$cell - 1][1]){
print "<div class=\"feaureitems\"><img src=\"images/squarebul.png\">&nbsp;".$items[$cell - 1][1]."</div>"; /* $items[$cell - 1][1] to display title $items[$cell - 1][2] etc... */
}
}
else {
$cell += $numrows;
if($items[$cell - 1][1]){
print "<div class=\"feaureitems\"><img src=\"images/squarebul.png\">&nbsp;".$items[$cell - 1][1]."</div>";
}
}



I still think there's something wrong with the way the arrays are being used,
but maybe I just don't know what your site is about. I just have a hunch
that you're making it much harder than it needs to be.


.

Fou-Lu
01-30-2012, 02:35 PM
Are you intending to display it effectively by column and then row, putting the padded results into the last column instead of the last row?
You don't need to actually capture the additional array if you don't need to use it outside of this fetch. If you do, then yeah an array would probably make the most sense.

dk4210
01-30-2012, 03:25 PM
That worked thanks!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum