Wrox Home  
Search P2P Archive for: Go

  Return to Index  

pro_php thread: SV: Displaying MySQL data across tables...


Message #1 by "Datatal AB - Gauffin, Jonas" <Jonas@d...> on Mon, 21 Oct 2002 08:20:34 +0200
Easy =3D)

You need two tables and 2 querys

//colcounter for the layouttable
$col =3D 0;

//layout table
Echo "<table>"
$resid =3D mysql_query("SELECT * FROM parent WHERE id=3D".$rs["id"]);
While ($rs =3D mysql_fetch_assoc($residParent))
{
	echo "<td>"
	$residSub =3D mysql_query("SELECT * FROM sub WHERE 
parentid=3D".$rs["id"]);

	//the "real" table
	echo "<table width=3D'100%'>";

	//PARENT X
	echo "<tr><td width=3D'100%'>".$rs["name"]."</td></tr>"

	//list all cubcats
	while ($rsSub =3D mysql_fetch_assoc($residSub))
	{
		echo "<tr><td>".$rsSub["name"]."</td></tr>";
	}
	echo "</table></td>"

	//When we have 2 columns, start on a new row
	if (++$col =3D 2)
	{
		echo "</tr><tr>";
		$col =3D 0;
	}
}

> -----Ursprungligt meddelande-----
> Fr=E5n: Kyle [mailto:bige88fan@c...]
> Skickat: den 19 oktober 2002 21:06
> Till: professional php
> =C4mne: [pro_php] Displaying MySQL data across tables...
>
>
> Hello again. THis one is a little easier than my previous
> questions...(I
> think/hope). Basically what I am trying to do is take parent
> categories
> and their sub-categories from MySQL data, and display them
> across tables.
> I know how to do it partially, but not the way that I need to
> do it. Here
> is what I am confused by.
>
> Lets say I have 4 Parent categories, each having their own
> sub-categories.
> I want them to be displayed in columns of 2. Sort of like this.
>
> PARENT 1 | PARENT 2
> -sub1    | -sub1
> -sub2    | -sub2
>
> ____________________
> PARENT3  | PARENT 4
> -sub1    | -sub1
> -sub2    | -sub2
>
> ____________________
>
> Can you guys understand that? hope so:)
>
> here is my current code for fetching the sub-category and
> category arrays:
>
> //Execute a couple of queries in order to generate categoriy
> parents w/subs $query =3D mysql_query("SELECT DISTINCT
> title,headingid FROM cat_headings
> WHERE game=3D'$game' ");
>
> while($cat1 =3D mysql_fetch_array($query)){
> $parent[] =3D $cat1["title"];
> $id[] =3D $cat1["headingid"];
> }
> mysql_free_result($query);
> unset($cat1,$query);
>
> $count =3D count($id);
>
> $dump_data .=3D "<table cellpadding=3D\"0\" cellspacing=3D\"0\"
> border=3D\"$get [bordersz]\" bordercolor=3D\"$get[borderclr]\"
> valign=3D\"top\"><tr>"; for($i=3D0;$i<$count;$i++){
>
> if (($counter++ % 2) !=3D 0) {
> 		$begin =3D "<td>";
>                 $end =3D "</td></tr>";
> 	} else {
> 		$begin =3D "<td>";
>                 $end =3D "</td>";
> 	}
>
> $dump_data .=3D "
> $begin<font size=3D\"3\" color=3D\"$get[pagetxt_color]\">$parent[$i]
> </font>$end";
>
> //within the loop, get categories belonging to parent
> $query =3D mysql_query("SELECT DISTINCT catname,catprefix FROM
> categories
> WHERE headingid=3D'$id[$i]' ORDER BY catname ASC");
> while($result =3D mysql_fetch_array($query)){
> $sub =3D $result["catname"];
> $pre =3D $result["catprefix"];
> $dump_data .=3D "
> <tr><td><font size=3D\"2\"><a href=3D\"$PHP_SELF?\" title=3D\"$sub\"
> style=3D\"Color: $get[pagetxt_bldcolor]; Font-Weight: Bold\">
>                $sub</a></font><br></td></tr>";
>
> 	}
>    }
> $dump_data .=3D "</table>";
> do_template($dump_data);
> }
>
> As you can see, I tried with the use of this code:
> if (($counter++ % 2) !=3D 0) {
> 		$begin =3D "<td>";
>                 $end =3D "</td></tr>";
> 	} else {
> 		$begin =3D "<td>";
>                 $end =3D "</td>";
> 	}
>
> To set up a endpoint-begin point marker for where the
> tables/<td>'s should
> end, but didn't work:( Help appreciated,
>
> -Kyle.
>
>
>
>

  Return to Index