Jump to content


Photo

need a loop or something like that


  • Please log in to reply
7 replies to this topic

#1 zirgs

zirgs
  • Members
  • PipPip
  • Member
  • 25 posts

Posted 11 June 2006 - 03:50 PM

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:
<?php

include '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>";
  
}
}

?>


#2 joquius

joquius
  • Members
  • PipPipPip
  • Advanced Member
  • 319 posts

Posted 11 June 2006 - 04:05 PM

umm better change some code here. I would no advise the following:

$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<?
}
}

OR = ||
<> = !=
z..z..z..z..z..z..z..z..

#3 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 11 June 2006 - 04:10 PM

If you want to show multiple results from database use a while loop ie:

while($row = mysql_fetch_array($result))
{
    //echo out results
}


#4 zirgs

zirgs
  • Members
  • PipPip
  • Member
  • 25 posts

Posted 11 June 2006 - 04:33 PM

Ok i changed my code to this one:
<?php

include '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>";
  
}
}

?>
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 ...

#5 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 11 June 2006 - 05:52 PM

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.

#6 zirgs

zirgs
  • Members
  • PipPip
  • Member
  • 25 posts

Posted 11 June 2006 - 06:37 PM

ok i'll make it "more english"
<?php

include '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 coment

echo "<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>";
  
}
}

?>
now you should understand which part is coment part ... any solutions?

#7 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 11 June 2006 - 07:16 PM

Looking at your code. it looks like you can do with a complete recode really. Like so:
<?php

include '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 it
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=" . $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>";
    }
}

?>


#8 zirgs

zirgs
  • Members
  • PipPip
  • Member
  • 25 posts

Posted 13 June 2006 - 06:39 AM

thanks, wildteen88.
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\" /]




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users