Jump to content

[SOLVED] For each loop?? how to do?


sudip_dg77

Recommended Posts

Hi,

 

I am kind of new to php, and i am getting a big problem on how to execute a php for each loop.

I can not get it to work any how.

 

I have a database table with many columns. I just want to pick a particular coulmn(user_id) from the table and print all the values in that column(user_id) row wise one by one.

 

So if my table is like this:

 

user_id fname lname email

1256 John Gone [email protected]

4526 Som Kya [email protected]

1422 Sab Man [email protected]

1236 Dadi Pet [email protected]

1233 Bob Shak [email protected]

4866 Mili Nan [email protected]

1232 Sam Hu [email protected]

 

I just want to pick up the user_id column and keep printing the values one by one.

 

Here is my code to do that.

 

<?php

$myDatabase = "kuuja_business";
$newline = "<br />";

$con = mysql_connect("localhost:3306","kuuja_admin","admin1");

if (!$con)

{


  die('Could not connect to Database: ' . mysql_error());


  }

@mysql_select_db($myDatabase, $con) or die("Unable to select database");


$report1 =  mysql_query("SELECT * FROM user");
$report2 =  mysql_fetch_array($report1);
@report3 =  $report2['user_id'];
echo "$report3";

[color=red]foreach $i (@report3)[/color]{
echo "User id: $i".$newline.$newline;
}



?>

 

Parse error: syntax error, unexpected T_VARIABLE, expecting '(' in /home/kuuja/public_html/test/test.php on line 26

 

Line 26 is highligted in red above.

 

 

But someone it is not working, but it is not working, either it is showing different errors or doesn't pick up the values one by one appropriately.

 

Can you help?

 

 

Thanks in advance

 

 

Link to comment
https://forums.phpfreaks.com/topic/88936-solved-for-each-loop-how-to-do/
Share on other sites

The syntax is

<?php
foreach ($array as $value)  // puts the value of each element in turn into $value
{
          echo $value . '<br>';
}

 

or

 

foreach ($array as $key => $value)  // puts the value of each element in turn into $value
                                                       and puts the elements key into $key. 
{
          echo $value . '<br>';
}

 

In your array in $report2 you have the fields from the first record so

 

foreach ($report2 as $val)
{
    echo $val . '<br/>';
}

gives

 

1256 

John 

Gone 

[email protected]

Yeah, but the code for getting rows out of a table is like so:

 

<?php
  $myDatabase = "kuuja_business";
  $newline = "<br />";

  $con = mysql_connect("<don't>","<post>","<this>");
  if (!$con){
    die('Could not connect to Database: ' . mysql_error());
  }

  @mysql_select_db($myDatabase, $con) or die("Unable to select database");

  $result =  mysql_query("SELECT * FROM user");
  while($row = mysql_fetch_array($result)){
    $userid = $row['user_id'];
    echo "User id: $userid".$newline.$newline;
  }
?>

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.