zirgs Posted June 11, 2006 Share Posted June 11, 2006 Hello i'm now using this scripts for news display but i need to show each news coments, but with this code i'm having trouble that it shows only 1 coment (but in database is more than one) ... can you help me out?code:[code]<?phpinclude 'db_con.php';$numb = $_GET['numb'];settype($numb,"integer");if(!empty($_GET['numb'])){$sql = mysql_query("select * from news where id = '$numb'");$num = mysql_num_rows($sql);$sql_k = mysql_query("select * from komentari where zinjuid = '$numb' order by id desc");if($num == 1){$row = mysql_fetch_array($sql);$row_k = mysql_fetch_array($sql_k);$t = $row["t"];$d = $row["d"];$n = $row["n"];$iz = $row["iz"];$au = $row_k["au"];$co = $row_k["co"];$cod = $row_k["cod"];echo "<span id=virs>";echo $t;echo "</span><br />";echo "<span id=dat>";echo $d;echo "</span><br />";echo $n;echo "<br /><br /><br /><br /><br />";echo "<span id=au>";echo $au;echo "</span><br />";echo "<span id=dat>";echo $cod;echo "</span><br />";echo $co;echo "<br /><br />";}}if(empty($_GET['numb']) OR $num <> 1){$sql = mysql_query("select * from zinjas order by id desc");while($row = mysql_fetch_array($sql)){echo "<span id=virs><a href=zinjas.php?numb=";echo $row["id"];echo ">";echo $row["t"];echo "</a></span><br />";echo "<span id=dat>";echo $row["d"];echo "</span><br />";echo $row["iz"];echo "<br /><br />";echo "<div class=komentari>";echo "<a href=zinjas.php>Komentari</a>";echo "</div>"; }}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/11718-need-a-loop-or-something-like-that/ Share on other sites More sharing options...
joquius Posted June 11, 2006 Share Posted June 11, 2006 umm better change some code here. I would no advise the following:[code]$sql = "SELECT * FROM `table` WHERE `field` = '$value'";$result = mysql_query ($sql);if (mysql_num_rows ($result) != 0){?>first html piece<?}else{while ($data = mysql_fetch_array ($result)){ ?>second html piece<?}}[/code]OR = ||<> = != Quote Link to comment https://forums.phpfreaks.com/topic/11718-need-a-loop-or-something-like-that/#findComment-44306 Share on other sites More sharing options...
wildteen88 Posted June 11, 2006 Share Posted June 11, 2006 If you want to show multiple results from database use a while loop ie:[code]while($row = mysql_fetch_array($result)){ //echo out results}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/11718-need-a-loop-or-something-like-that/#findComment-44308 Share on other sites More sharing options...
zirgs Posted June 11, 2006 Author Share Posted June 11, 2006 Ok i changed my code to this one:[code]<?phpinclude 'db_con.php';$numb = $_GET['numb'];settype($numb,"integer");if(!empty($_GET['numb'])){$sql = mysql_query("select * from news where id = '$numb'");$num = mysql_num_rows($sql);$sql_k = mysql_query("select * from komentari where zinjuid = '$numb' order by id desc");if($num == 1){$row = mysql_fetch_array($sql);$row_k = mysql_fetch_array($sql_k);$t = $row["t"];$d = $row["d"];$n = $row["n"];$iz = $row["iz"];$au = $row_k["au"];$co = $row_k["co"];$cod = $row_k["cod"];echo "<span id=virs>";echo $t;echo "</span><br />";echo "<span id=dat>";echo $d;echo "</span><br />";echo $n;echo "<br /><br /><br /><br /><br />";while($row_k = mysql_fetch_array($sql_k)){echo "<span id=au>";echo $au;echo "</span><br />";echo "<span id=dat>";echo $cod;echo "</span><br />";echo $co;echo "<br /><br />";}}}if(empty($_GET['numb']) OR $num <> 1){$sql = mysql_query("select * from zinjas order by id desc");while($row = mysql_fetch_array($sql)){echo "<span id=virs><a href=zinjas.php?numb=";echo $row["id"];echo ">";echo $row["t"];echo "</a></span><br />";echo "<span id=dat>";echo $row["d"];echo "</span><br />";echo $row["iz"];echo "<br /><br />";echo "<div class=komentari>";echo "<a href=zinjas.php>Komentari</a>";echo "</div>"; }}?>[/code]it still echoes out only one comment ... maybe i'm using while in this case wrong? sorry i'm not yet familiar with php where is involved mysql ... Quote Link to comment https://forums.phpfreaks.com/topic/11718-need-a-loop-or-something-like-that/#findComment-44313 Share on other sites More sharing options...
wildteen88 Posted June 11, 2006 Share Posted June 11, 2006 What part of that code is the comments bit? Or is that the actual comment code? If it is why is there 3 or 4 queies to get the comments out of the database. Quote Link to comment https://forums.phpfreaks.com/topic/11718-need-a-loop-or-something-like-that/#findComment-44334 Share on other sites More sharing options...
zirgs Posted June 11, 2006 Author Share Posted June 11, 2006 ok i'll make it "more english"[code]<?phpinclude 'db_con.php';$numb = $_GET['numb'];settype($numb,"integer");if(!empty($_GET['numb'])){$sql = mysql_query("select * from news where id = '$numb'");$num = mysql_num_rows($sql);$sql_coments= mysql_query("select * from coments where newsid = '$numb' order by id desc");if($num == 1){$row = mysql_fetch_array($sql);$row_coments = mysql_fetch_array($sql_coments);$title = $row["title"];$date = $row["date"];$new = $row["new"];$shortnew = $row["shortnew"];$author = $row_k["author"]; // coment author$coment = $row_k["coment"]; // coment$comentdate = $row_k["comentdate"]; // date of the comentecho "<span id=virs>";echo $title;echo "</span><br />";echo "<span id=dat>";echo $date;echo "</span><br />";echo $new;echo "<br /><br /><br /><br /><br />";while($row_coments = mysql_fetch_array($sql_coments)){echo "<span id=au>";echo $author;echo "</span><br />";echo "<span id=dat>";echo $comentdate;echo "</span><br />";echo $coment;echo "<br /><br />";}}}if(empty($_GET['numb']) OR $num <> 1){$sql = mysql_query("select * from zinjas order by id desc");while($row = mysql_fetch_array($sql)){echo "<span id=virs><a href=zinjas.php?numb=";echo $row["id"];echo ">";echo $row["title"];echo "</a></span><br />";echo "<span id=dat>";echo $row["date"];echo "</span><br />";echo $row["shortnew"];echo "<br /><br />";echo "<div class=komentari>";echo "<a href=zinjas.php>Coments</a>";echo "</div>"; }}?>[/code]now you should understand which part is coment part ... any solutions? Quote Link to comment https://forums.phpfreaks.com/topic/11718-need-a-loop-or-something-like-that/#findComment-44363 Share on other sites More sharing options...
wildteen88 Posted June 11, 2006 Share Posted June 11, 2006 Looking at your code. it looks like you can do with a complete recode really. Like so:[code]<?phpinclude 'db_con.php';$numb = ( ( isset($_GET['numb']) && is_int($_GET['numb']) ) ? $_GET['numb'] : die("hacking attempt"));$sql_n = "SELECT * FROM news WHERE id = '$num' LIMIT 1";$result_n = mysql_query($sql_n);if(mysql_num_rows($result_n) >= '1'){ $news = mysql_fetch_array($result_n); $title = $news["title"]; $date = $news["date"]; $new = $news["new"]; $shortnew = $news["shortnew"]; echo "<span id=virs>" . $title . "</span><br />"; echo "<span id=dat>" . $date . "</span><br />"; echo $new; echo "<br /><br /><br /><br /><br />"; $sql_c = "SELECT * FROM comments WHERE newsid='$numb' ORDER BY id DESC"; $result_c = mysql_query($sql_c); if(mysql_num_rows($result_c) < '1') { echo "no comments in database"; } else { while($comments = mysql_fetch_array($sql_c)) { $author = $comments["author"]; // coment author $coment = $comments["coment"]; // coment $comentdate = $comments["comentdate"]; // date of the coment echo "<span id=au>" . $author . "</span><br />"; echo "<span id=dat>" . $comentdate . "</span><br />"; echo $coment . "<br /><br />"; } }}//not sure what this was so I left itif(empty($_GET['numb']) OR $num <> 1){ $sql = mysql_query("select * from zinjas order by id desc"); while($row = mysql_fetch_array($sql)) { echo "<span id=virs><a href=zinjas.php?numb=" . $row["id"] . ">" . $row["title"] . "</a></span><br />"; echo "<span id=dat>" . $row["date"] . "</span><br />"; echo $row["shortnew"]; echo "<br /><br />"; echo "<div class=komentari><a href=zinjas.php>Coments</a></div>"; }}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/11718-need-a-loop-or-something-like-that/#findComment-44372 Share on other sites More sharing options...
zirgs Posted June 13, 2006 Author Share Posted June 13, 2006 thanks, [b]wildteen88[/b].I've just modified it a bit because there was one error ... but now my problem is solved.cheers! [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /] Quote Link to comment https://forums.phpfreaks.com/topic/11718-need-a-loop-or-something-like-that/#findComment-44880 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.