Special formatting for data retrieved from a MySQL table
Posted: Mon Aug 21, 2006 8:15 am
feyd | Please use
My ideea to achieve this is putting the result from the query in two dimensional array. I tried using mysql_fetch_array (object, row) and using then next() and current() but I got the error message that the arrays I was storing the data from the table aren' t arrays (someone told me that mysql_fetch_* returns a reference to the field of data than the actual field). I tried to return a object and his properties to be displayed the way I wanted but the array remembered only the last entry in MySQL field.
Here is the code :
feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
I have two tables one with the different product' s categories and the other with subcategories of a category. The second one is linked to the first one by a foreign to the first. The query uses the foreign key to link the names of the two tables togheter.
I want to display the name of a category on top of his subcategories and the same to different category. Something like this:Code: Select all
-----------------------------
| Category 1 |
|-------------------------- - |
| Subcategory 1.1 |
| Subcategory 1.2 |
| ----------------------------|
|---------------------------- |
| Category 2 |
-------------------------------
| Subcategory 2.1 |
| Subcategory 2.2 |
-------------------------------
----------------------------------
Category 3
--------------------------------
---------------------------------Here is the code :
Code: Select all
connect(); //function which connects to the database
$sql = 'SELECT DISTINCT CATEGORIES.NumCat AS Category, SUBCATEGORIES.NumSub as Subcategory
FROM CATEGORIES INNER JOIN SUBCATEGORIES
ON CATEGORIES.C= SUBCATEGORIES.Cat
ORDER BY NumeCat, NumSub';
$rs = mysql_query($sql) or die(mysql_error());
$n=mysql_num_rows($rs);
$i=1;
while($rand=mysql_fetch_object($rs) && Si<$n)
{
$cat[$i]=$rand -> Categorie;
$subcat[$i]=$rand -> Subcategorie;
}
if (current($cat["NumCat"]) != next($cat["NumCat"]))
{
if (!$first)
{
echo '</ul>';
}
echo $cat["NumCat"] .'<ul>';
$prev = $row["NumCat"];
}
echo '<li>' .$row["NumSub"] .'</li>';
$first = false;
}
echo '</ul>';*/
do
{
echo "<tr>";
if ($cat[$i]!= $cat[$j])
{
echo "<td>" .$cat[$i];
if ( $subcat[$i] != '')
{}
echo "<td>"$subcat[$i] ."</td>";
echo "</tr>";
else
{
if ( $subcat[$i] != '')
{
echo "<td>";
echo $subcat[$i]) ."</td>";
echo "</tr>";
}
}
$j=$i;
$i=$i+1;
}
while($i<$n && $j<$n-1 );
echo "</table>";
mysql_free_result();
}feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]