mysqlresult->x rows->row[x]

eyz

eyz

Aktives Mitglied
Thread Starter
Dabei seit
29.12.2003
Beiträge
180
Reaktionspunkte
0
hallo allerseits!

-----------------------------------mein problem:
ich habe 2tabellen->
cats[ID]
forums[ID,CID]
..und will sie so ausgeben->
cats(ID=1)
forums(ID=1,CID=1)
forums(ID=3,CID=1)
cats(ID=2)
forums(ID=2,CID=2)
forums(ID=4,CID=2)

----------------------------------mein lösungsansatz:
PHP:
	include("../config.php");
	include("../login.php");
	@mysql_select_db($db_bp,$dbcnx_bp)
		or die("Abbruch: Datenbank ".$db_bp." konnte nicht".
		" selektiert werden.<br><br>MySQL sagt: ".mysql_error

	$sql = "SELECT c.ID AS cid, c.name AS cname, c.name AS cname
		FROM cats c, forums f
		WHERE f.CID=c.ID
		GROUP BY c.ID, c.name";
	if(!$cats = mysql_query($sql)){
		die("ERROR['$sql'] SQL[".mysql_error()."]");
	}
	$totalcats = mysql_num_rows($cats);
	while($catrow = mysql_fetch_array($cats)){
		$cats[] = $catrow;
	}
	$sql = "SELECT f.ID AS fid, f.name AS fname, f.about AS fabout
		FROM forums f, cats c
		WHERE f.CID=c.ID";
	if(!$forums = mysql_query($sql)){
		die("ERROR['$sql'] SQL[".mysql_error()."]");
	}
	$totalforums = mysql_num_rows($forums);
	while($forumrow = mysql_fetch_array($forums)){
		$forums[] = $forumrow;
	}
//------------------------------------------------------------------------>ausgabe		
	for($i = 0; $i < $totalcats; $i++) {
		$cid = stripslashes($cats[$i][cid]);
		$cname = stripslashes($cats[$i][cname]);
		$cabout = stripslashes($cats[$i][cabout]);
		echo("<div class='post'><table width='100%'><tr><td class='noborder' align='left'>".
			"<a href='showcat.php?cid=$cid'>$cname</a> $cabout</td></tr>".
			"<tr><td class='noborder'>[<a href='newforum.php?cid=$cid'>create new forum</a>]</td></tr></table>");
		echo("<table width='100%'><tr><th>iconshice</th><th>topic</th>".
			"<th>lastreply</th><th>threads</th><th>posts</th><th>mod</th></tr>");

		for($x = 0; $x < count($forums); $x++){
			$fid = $forums[$x]["fid"];
			$fname = stripslashes($forums[$x][fname]);
			$fabout = stripslashes($forums[$x][fabout]);
			echo("<tr><td align='middle' width='50px'>icons</td><td align='left'>".
				"<a href='showforum.php?fid=$fid'>$fname</a><br>$fabout</td>".
				"<td align='right'>lastreply</td><td align='middle'>threads</td>".
				"<td align='middle'>posts</td><td align='middle'>mod</td></tr>");
			echo("</table></div>");	
		}
	}

...ich komm nicht drauf, verwende zum ersten mal ein array für ganze "mysql-rows"
bin mir nicht sicher ob ich mit $array[$i][spaltenname] eine zelle auslesen kann

bitte um hilfe :)
 
Ich hab leider nur wenig Zeit, aber Deine beiden SQL-Abfragen sind etwas strange:

Du verwendest einen impliziten join auf zwei Tabellen über die id's,
hast aber in den Result-Set immer nur eine Tabelle enthalten. Wozu dann der join?
Verstehe ich das richtig, daß Du zu jeder ID aus cats alle forum.CID haben willst die dieser entsprechen? Dann wäre es doch etwa

select c.*, f.* from cats c, forum f
where c.id=f.cid
group by c.id
order by c.id

Wobei du die Spalten im select noch anpassen müßtest und dazu dann die Angaben bei group by. Verzeih mir Fehler, war eine schnelle Antwort zwischendurch...
 
thx!! funktioniert einwandfrei

falls wem interessiert:
PHP:
	$sql = "SELECT c.*, f.*, c.ID AS cid, c.name AS cname, c.about AS cabout, f.ID AS fid, f.name AS fname, f.about AS fabout
		FROM cats c, forums f
		WHERE c.ID=f.CID
		GROUP BY c.ID
		ORDER BY c.ID";
	if(!$cats = mysql_query($sql)){
		die("ERROR['$sql'] SQL[".mysql_error()."]");
	}
	$totalcats = mysql_num_rows($cats);
	while($cat = mysql_fetch_array($cats)){
//------------------------------------------------------------------------> ausgabe		
		$cid = ($cat["cid"]);
		$cname = ($cat["cname"]);
		$cabout = ($cat["cabout"]);
		$fid = ($cat["fid"]);
		$fname = ($cat["fname"]);
		$fabout = ($cat["fabout"]);
		echo("<div class='post'><table width='100%'><tr><td class='noborder' align='left'>".
			"<a href='showcat.php?cid=$cid'>$cname</a> $cabout</td></tr>".
			"<tr><td class='noborder'>[<a href='newforum.php?cid=$cid'>create new forum</a>]</td></tr></table>");
		echo("<table width='100%'><tr><th>iconshice</th><th>topic</th>".
			"<th>lastreply</th><th>threads</th><th>posts</th><th>mod</th></tr>");
		echo("<tr><td align='middle' width='50px'>icons</td><td align='left'>".
			"<a href='showforum.php?fid=$fid'>$fname</a><br>$fabout</td>".
			"<td align='right'>lastreply</td><td align='middle'>threads</td>".
			"<td align='middle'>posts</td><td align='middle'>mod</td></tr>");
		echo("</table></div>");
	}
 
Zurück
Oben Unten