-2

i am tring to put a loop to echo a number inside an echo ; and i tried as bellow :

     $array = array();
     $result = mysql_query("SHOW TABLES FROM `st_db_1`");
     while($row = mysql_fetch_row($result) ){
     $result_tb = mysql_query("SELECT id FROM $row[0] LIMIT 1");
     $row_tb=mysql_fetch_array($result_tb);
     $array[] = $row[0];
     $array2[] = $row_tb[0];
     //checking for availbility of result_tb
     /*   if (!$result_tb) {
                  echo "DB Error, could not list tablesn";
                  echo 'MySQL Error: ' . mysql_error();
                  exit;
                  }  */
     }
     natsort($array);
     natsort($array2);

    foreach ($array as $item[0]  ) {
    echo "<a href=show_cls_db.php?id= foreach ($array2 as $item2[0]){echo \"$item2[0]\"}>{$item[0]}<br/><a/>" ;
    }

but php is not considering foreach loop inside that echo ; please suggest me something

Kristiono Setyadi
  • 5,635
  • 1
  • 19
  • 29
ravi
  • 109
  • 2
  • 4
  • 13

4 Answers4

4

As mentioned by others, you cannot do loops inside a string. What you are trying to do can be achieved like this:

foreach ($array as $element) {
    echo "<a href='show_cls_db.php?id=" . implode('', $array2) . "'>{$element}</a><br/>";
}

implode(...) concatenates all values of the array, with a separator, which can be an empty string too.

Notes:

  1. I think you want <br /> outside of <a>...</a>
  2. I don't see why you would want to used $item[0] as a temporary storage for traverser array elements (hence renamed to $element)
user229044
  • 232,980
  • 40
  • 330
  • 338
mkilmanas
  • 3,395
  • 17
  • 27
3

Just use implode instead of trying to loop the array,

foreach ($array as $item)
{
    echo implode("",$array2);
}

other wise if you need to do other logic for each variable then you can do something like so:

foreach ($array as $item)
{
    echo '<a href="show_details.php?';
    foreach($something as $something_else)
    {
        echo $something_else;
    }

    echo '">Value</a>';
}

we would have to see the contents of the variables to understand what your trying to do.


As a wild guess I would think your array look's like:

array(
     id => value
)

And as you was trying to access [0] within the value produced by the initial foreach, you might be trying to get the key and value separate, try something like this:

foreach($array as $id => $value)
{
     echo $id;    //This should be the index
     echo $value; //This should be the value
}
RobertPitt
  • 56,863
  • 21
  • 114
  • 161
  • i have put this code in a function and whenever i call this fuction it should display a list of school grade ; – ravi Jun 01 '11 at 08:01
  • 1
    that means nothing to us @ravi, we need to see the context, show us examples of the array and the desired output ! – RobertPitt Jun 01 '11 at 08:03
  • i will be getting an array which contain (1,2,3,4,5,6,7,8,9,10,11,12) now i want those no. to be my urls id so that on the next page i can pull all the content related to a no.. – ravi Jun 01 '11 at 09:40
0
foreach ($array as $item  ) {
    echo "<a href=\"show_cls_db.php?id=";
    foreach ($array2 as $item2) { echo $item2[0]; }
    echo "\">{$item[0]}<br/><a/>" ;
}
Adam Lynch
  • 3,341
  • 5
  • 36
  • 66
0

No offense but this code is... rough. Post it on codereview.stackexchange.com for some help re-factoring it. A quick tip for now would be to use PDO, and at the least, escape your inputs.

Anyway, as the answers have pointed out, you have simply echoed out a string with the "foreach" code inside it. Take it out of the string. I would probably use implode as RobertPitt suggested. Consider sorting and selecting your data from your database more efficiently by having mysql do the sorting. Don't use as $item[0] as that makes absolutely no sense at all. Name your variables clearly. Additionally, your href tag is malformed - you may not see the correct results even when you pull the foreach loop out of the echo, as your browser may render it all away. Make sure to put those quotes where they should be.

Aaria Carter-Weir
  • 1,639
  • 12
  • 17