Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

php mysql_fetch_array 2

Status
Not open for further replies.

styleBunny

Technical User
Jun 20, 2002
133
AU
Hi everybody,

I have a simple table in my database that holds my website content (text), on my page i am running this query to get the text from the database.

"SELECT * FROM `content`"

Is there a way from this point I can iterate through each piece of content, and display them at different locations on the page?

I guess im trying to avoid having to do 3 or more seperate queries. I have in the past used mysql_fetch_array with a while loop, but this will only list all the results. I want to be able to insert one result at the top of the page and other results further down the page.

Cheers,
SB.
 
read all of the results into an array and use as you will in the page
Code:
$i=0;
while ($row=mysql_fetch_assoc($result))
{
  foreach($row as $key=>$val)
  {
    $tmparray[$i][$key]=$val;
  }
  $i++;
}
 
Hi jpadie,

i think i under stand...

$tmparray[$i][$key]=$val;

The above line is building the array ($tmparray), is that true?

Im not sure how to then retrieve the results in the array,
echo $tmparray[1];, displays the word 'array'.

I think im half right, but not sure which half :)

thanks again,
sb.

 
Actually, you don't even have to have the index "$i" in the loop:
Code:
$tmparray = array();
while ($row=mysql_fetch_assoc($result))
  foreach($row as $key=>$val)
    $tmparray[][$key]=$val;
After you do this you will have a temporary array with as many first indicies as you had rows in the database, each of the indicies points to an array consisting of the fields of each row. If you do a
Code:
echo '<pre>';print_r ($tmparray);echo '</pre>';
you will see the contents.

Ken
 
sorry - been away.

the tmparray is structured with a numeric first index. thus you would need to know the position of the item you wanted (or search for it) at each point.

if your table has a unique key that is meaningful then you could adapt the code thus:

Code:
$uniquekeycolumnname="uid";
while ($row=mysql_fetch_assoc($result))
{
  
 foreach($row as $key=>$val)
  {
    if ($key != $uniquekeycolumnname)
    { 
      $tmparray[$uniquekeycolumnname][$key]=$val;
    }
  }
}

then let's say your table has the other columns "First_Name" and "Last_Name"

you would then use the tmparray thus:

Code:
//to output uid 3
echo $tmparray['3']['First_Name'] . " " . $tmparray['3']['Last_Name'];
 
Thanks again guys,

kenrbnsn, i got the print_r to display the array, in an array like formatt, but what i need is just the content, from the table, is there a way to put that into a variable and print that maybe?

jpadie, i think what i mentioned above is achieved by your suggestion but i cant get it to work, the line:

$uniquekeycolumnname="uid";

I tried setting the "uid" to my table's PK, "content_id" and the line

echo $tmparray['3']['First_Name'] . " " . $tmparray['3']['Last_Name'];

To the table's other attributes, but it wouldn't print anything.

Thanks again for your help, i am getting a handle on this now. really, i am :)

Cheers,
Paul.
 
sorry, guilty of falling into one of my own traps.

the code should have been
Code:
$uniquekeycolumnname="uid";
 foreach($row as $key=>$val)
  {
    if ($key != $uniquekeycolumnname)
    {
      $tmparray[$row[$uniquekeycolumnname]][$key]=$val;
    }
  }

the problem was that i was not referencing the row value of the uid column but the uid column name. doh.
 
Hi Jpadie,

Thats better, thanks for that. Though im not 100% clear on _how_ this works, its certainly does work. I shall have to look into some more php thoery me thinks. :)

Its neat that the tmparray matches with the row id from the database, as opposed to starting at 0.

Thanks guys,
SB.
 
here's how it works

Code:
$uniquekeycolumnname="uid";
while ($row=mysql_fetch_assoc($result))  //this iterates through the recordset row by row and reads all the columns into an array called $row. you can address the result directly here by using the syntax $row['column_name']
{
  
 foreach($row as $key=>$val)  // this takes the $row array and allows you easily to address each key->value pair in the array iteratively.  if you *knew* each key->value pair you could address them individually but this way is dynamic and easy to use.
  {
    if ($key != $uniquekeycolumnname)  //because the unique id is a key->value pair it will also be presented by the foreach loop.  but we don't need it to form part of the value in the array.  instead we use it as the master key for each 'row'
    {
      $tmparray[$uniquekeycolumnname][$key]=$val; //this is a way of using string literals as array keys.  normally array keys are numeric.  but you can use strings.  because the value of uniquecolumnname is definitionally unique this is safe.  if it is not unique then the second call would overwrite the first etc (redeclaring the same variable).  
//this notation creates a multidimensional array
    }
  }
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top