Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

foreach in php

lucky_melucky_me Posts: 39Member
i want to use "foreach " loop statement in my php program.instead of taking array i want to use the resultant query(taken from the database) is this possible to do so.
like...

foreach($result as $key=>$value){
// statement to print the records.

I am confused....i think when we fetch some records from database and stores it in any variable . it's (the variable is)an array in itself. I may be wrong.If i am ...then please correct me.

Thanks in advance.

Comments

  • skylarskylar Posts: 82Member
    [b][red]This message was edited by skylar at 2004-7-26 8:53:8[/red][/b][hr]
    : i want to use "foreach " loop statement in my php program.instead of taking array i want to use the resultant query(taken from the database) is this possible to do so.
    : like...
    :
    : foreach($result as $key=>$value){
    : // statement to print the records.
    :
    : I am confused....i think when we fetch some records from database and stores it in any variable . it's (the variable is)an array in itself. I may be wrong.If i am ...then please correct me.
    :
    : Thanks in advance.
    :
    :


    This is the way to retreive information from a database:


    $query = mysql_query("SELECT * FROM ");

    while($sqldata = mysql_fetch_row($query)){
    .. do stuff
    }

    mysql_fetch_row gives you a Multi-Dimensional array consisting of the rows then the columns. If you want to grab information from this multi-dimensional array out of order then you will need to create your own multi-dimensional array.

    $mydata = array();
    for($rownum=0; $sqldata = mysql_fetch_row($query); $rownum++){
    $mydata[$rownum] = $sqldata;
    }

    So then, if you want to grab the 2nd column of the 40th row you can do this:

    echo $mydata[1][39];



    But most of the time you don't want to store the data in an array. You want to go through each and every row.. analize the row then echo data out, whatever you want to do.


    Reference: http://us2.php.net/manual/en/function.mysql-fetch-row.php


  • lucky_melucky_me Posts: 39Member
    : [b][red]This message was edited by skylar at 2004-7-26 8:53:8[/red][/b][hr]
    : : i want to use "foreach " loop statement in my php program.instead of taking array i want to use the resultant query(taken from the database) is this possible to do so.
    : : like...
    : :
    : : foreach($result as $key=>$value){
    : : // statement to print the records.
    : :
    : : I am confused....i think when we fetch some records from database and stores it in any variable . it's (the variable is)an array in itself. I may be wrong.If i am ...then please correct me.
    : :
    : : Thanks in advance.
    : :
    : :
    :
    :
    : This is the way to retreive information from a database:
    :
    :
    : $query = mysql_query("SELECT * FROM ");
    :
    : while($sqldata = mysql_fetch_row($query)){
    : .. do stuff
    : }
    :
    : mysql_fetch_row gives you a Multi-Dimensional array consisting of the rows then the columns. If you want to grab information from this multi-dimensional array out of order then you will need to create your own multi-dimensional array.
    :
    : $mydata = array();
    : for($rownum=0; $sqldata = mysql_fetch_row($query); $rownum++){
    : $mydata[$rownum] = $sqldata;
    : }
    :
    : So then, if you want to grab the 2nd column of the 40th row you can do this:
    :
    : echo $mydata[1][39];
    :
    :
    :
    : But most of the time you don't want to store the data in an array. You want to go through each and every row.. analize the row then echo data out, whatever you want to do.
    :
    :
    : Reference: http://us2.php.net/manual/en/function.mysql-fetch-row.php
    :
    Hi skylar, thanks for your response.
    Yeah you are right i can access the data by mysql_fetch_row() statement.Butin this case i have to use the field names of the tabel to display or perform some operation over it.I don't want to do this.
    I read about foreach that...its the simplest way of fetching data and is very useful, but it works only with array.If i use this looping construct then it's not necessary to handle every field present in that array individually .It takes "key" and "value" argument and fetches the field and its corresponding value.
    I wanted to knowthat ... does "foreach" handles just a double dimensional array or multiple dimensional array? and if it handles multidimensionla aray then can we use it to output the fetched fields (either we use mysql_fetch_row() or something else)?
    As ithink when we do query like
    $result=mysql_query(select * from employee);
    whatever the result rtrieved, that stored in a multidimensional array $result (am i correct)? and if foreach can handle multidimensional arraythen it can handle $result also.
    I have not somuch idea about it .I am a newbie so please correct me if i am wrong.
    Thanks alot.




  • skylarskylar Posts: 82Member
    [b][red]This message was edited by skylar at 2004-8-9 9:18:23[/red][/b][hr]
    ummmm no..

    a foreach loop can ONLY HANDLE ONE ARRAY DIMENSION :)


    but listen.. if you have a multi-dimensional array.. the first array HOLDS another array.. so when you do a foreach of the first array, the foreach will spit out the 2nd array as the value, eg:

    [code]

    $firstarr = array(array(1, 2, 3), array(4, 5, 6));

    foreach($firstarr as $secondarr){
    // $secondarr = array(1, 2, 3) .. then array(4, 5, 6)
    foreach($secondarr as $arrvalue){
    echo $arrvalue;
    }
    }

    output:
    123456
    [/code]



    Oh yah.. mysql_query returns a RECORD SET.. record sets can only be accessed via mysql_fetch_row.. fetch_array.. fetch_assoc.. or fetch_object


    You need to use these functions to access the data from a record set created by mysql_query.



    Use this as an example of how it should be done:

    [code]

    -> id, field1, field2, field3

    $q = mysql_query("SELECT * FROM ");
    while($data = mysql_fetch_row($q)){
    echo "id = $data[0], field1 = $data[1], field2 = $data[2], field3 = $data[3]";
    }

    [/code]

    hope that helps...




Sign In or Register to comment.