0

I need a little help here with some php. just a little explanation: (im trying to display 5 results using this code

    $n_id = mysql_real_escape_string ($_GET['id']);
    $path = '';

    if(isset($n_id) && $n_id != "") {
        $sql = 'SELECT * FROM test2 WHERE id="' . $n_id . '"';
        $news = mysql_query($sql);

    if($result = mysql_fetch_array($news)) {
        $title = mysql_result($news,0,"title");
        $date = mysql_result($news,0,"date");
        echo '<b>' . $title . ' | ' . $date . '</b>
        <br/>
        <img src="images.php?id='. $n_id .'>';
    } else {
        header("Location: vendi.php");
    }
    echo '<br /><a href="/'.$path.'">Back to Archive</a>';
    }

It does display but i want that 1st result to be (image+title of the news and other results to be just title). Hope i wrote it clearly what i needed help with. Thank you

3 Answers 3

1

Your SQL statement is only fetching a single row. This isn't a complete solution, but should get you closer:

if (isset($_GET['id']) && is_numeric($_GET['id'])) {
    $n_id = (int)$_GET['id'];
    $path = '';

    $count = 0;

    $sql = 'SELECT * FROM test2 WHERE id BETWEEN ' . $n_id ' AND ' . ($n_id + 5);
    $news = mysql_query($sql);

    while ($result = mysql_fetch_array($news)) {
        $title = $result['title'];
        $date = $result['date'];
        echo '<b>' . $title . ' | ' . $date . '</b>';
        if ($count < 1) {
            echo '<br/><img src="images.php?id='. $n_id .'>';
            $count++;
        }
    }

    if ($count == 0) { header("Location: vendi.php"); }

    echo '<br /><a href="/'.$path.'">Back to Archive</a>';
}
Sign up to request clarification or add additional context in comments.

1 Comment

THANKS YOUR ANSWER HELPED VERY MUCH TO DO THE JOB i modified my self but you were the head start thnx
0

This should do it.

$n_id = mysql_real_escape_string ($_GET['id']);
    $path = '';

    if(isset($n_id) && $n_id != "") {
        $sql = 'SELECT * FROM test2 WHERE id="' . $n_id . '"';
        $news = mysql_query($sql);

        $first = TRUE;
        if($result = mysql_fetch_array($news)) {
            $title = mysql_result($news,0,"title");
            $date = mysql_result($news,0,"date");

            if($first == TRUE) {
                echo '<b>' . $title . ' | ' . $date . '</b>';
                $first = FALSE;
            }
            else {
                echo '<b>' . $title . ' | ' . $date . '</b>
                <br/>
                <img src="images.php?id='. $n_id .'>';
            }
        }
        else {
            header("Location: vendi.php");
        }
        echo '<br /><a href="/'.$path.'">Back to Archive</a>';
        }
    }

Comments

0

While looping through your results, you can check if you are at the first result:

$counter = 0;
while ($row = mysql_fetch_array($news)) {
    $title = mysql_result($news,0,"title");
    $date = mysql_result($news,0,"date");
    echo '<b>' . $title . ' | ' . $date . '</b>';
    if ( $counter == 0 ) {
        echo '<br /><img src="images.php?id='. $n_id .'>';
    }

}

3 Comments

You're missing a close to your echo. I think he's going to have to switch up the SQL call too. Looks like he's doing a single row atm. That could get tetchier without more info.
You're assigning a value here: if ( $counter = 0 ) { Should be ==
Fixed, and fixed. Thanks. As to the single row issue, he said he is trying to display five results.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.