Jump to content

need a loop or something like that


zirgs

Recommended Posts

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]
<?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>";
  
}
}

?>[/code]
Link to comment
https://forums.phpfreaks.com/topic/11718-need-a-loop-or-something-like-that/
Share on other sites

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 = ||
<> = !=
Ok i changed my code to this one:
[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 />";
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 ...
ok i'll make it "more english"
[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_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>";
  
}
}

?>
[/code]
now you should understand which part is coment part ... any solutions?
Looking at your code. it looks like you can do with a complete recode really. Like so:
[code]<?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>";
    }
}

?>[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.