Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

How can I echo out value after the while loop. If I do the echo in below code its says Undefined index.

$sql_cast = "SELECT *
            FROM
              title_cast
              INNER JOIN title ON (title_cast.id_title = title.id)
              INNER JOIN `cast` ON (title_cast.id_cast = `cast`.id)
            WHERE
              title_cast.id_title = '1'";
$result_cast = mysql_query($sql_cast) or die('log error with' .mysql_error());
$cast = array();
while ($row = mysql_fetch_assoc($result_cast)) {
                $id = $row['id'];
                $name = $row['name'];
                $img = $row['photo_localurl'];
                $poster = str_replace("./", "lib/", $img);

                $cast[] = array('id' => $id, 'name' => $name, 'img' => $poster);
                //$cast[] = $row;
                }
                //var_dump($cast);
                echo $cast['id'] . " " . $cast['name'] . " " . $cast['poster']."<br />";
share|improve this question
 
Try var_dump($row) first in the while loop. Also, I would expect while( ($row = mysql_fetch_assoc($result_cast)) !== FALSE ) { or similar –  Aram Kocharyan Feb 3 '12 at 8:57
add comment

6 Answers

up vote 1 down vote accepted

Within the while loop, you set the cast array content using $cast[] syntax. This will create a numerical index, starting at 0, then 1 and so on, so you're creating an array that looks like this:

$cast = array(
    0 => array('id' => $id, 'name' => $name, 'img' => $poster),
    1 => array('id' => $id, 'name' => $name, 'img' => $poster)
);

You need to include the numerical key of the array that you want to echo. For example, if you want to echo the first row:

echo $cast[0]['id']; // Echo the id of the first result

If you want to echo ALL of the rows, use foreach:

foreach($cast as $row) {
    echo $row['id'];
}
share|improve this answer
 
foreach loop does the work. Thanks for the help to all. –  Bhaskar Wankhede Feb 3 '12 at 9:09
add comment

Maybe you should do:

$cast = array('id' => $id, 'name' => $name, 'img' => $poster);
                //$cast[] = $row;
                }
                //var_dump($cast);
echo $cast['id'] . " " . $cast['name'] . " " . $cast['poster']."<br />";

because if you use $cast[], it will append the new array to your array..

share|improve this answer
add comment

That's because you are pushing a new array in $cast at each index..

So you should echo like this..

$i = 0;
while ($row = mysql_fetch_assoc($result_cast)) {
      $id = $row['id'];
      $name = $row['name'];
      $img = $row['photo_localurl'];
      $poster = str_replace("./", "lib/", $img);

      $cast[] = array('id' => $id, 'name' => $name, 'img' => $poster);
      //$cast[] = $row;

      //var_dump($cast);
      echo $cast[$i]['id'] . " " . $cast[$i]['name'] . " " . $cast[$i]['poster']."<br />";
      $i++;
}
share|improve this answer
 
Note that I haven't tested this, but it should work... –  ThomasM Feb 3 '12 at 8:59
add comment

Try this:

<?php
while ($row = mysql_fetch_assoc($result_cast)) {
      $id = $row['id'];
      $name = $row['name'];
      $img = $row['photo_localurl'];
      $poster = str_replace("./", "lib/", $img);

      $cast[] = array('id' => $id, 'name' => $name, 'img' => $poster);
}

foreach($cast as $rows) {
    echo $rows['id']." ".$rows['name']." ".$rows['img']."<br />";
}
?>
share|improve this answer
add comment
print_r($cast[0]); or you can use $cast[0]['id'] ;
share|improve this answer
add comment

There are two problems with your code, First is that $cast[] is a two-dimensional array so it contains arrays at each of its index, so you must use

$cast[$i]['id'] 

Where i will be the counter variable that will iterate through all the indexes. Secondly change

$cast['poster'] 

to

$cast['img']

Hope this helps.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.