Jump to content

loop through recordset


adambedford

Recommended Posts

In my mind this is slightly more tricky than the usual while loop through a recordset, however I may be missing something really simple.

 

I have a table (tbl_Metadata) that stores metadata about every page of my site. Each page has an ID, so there is a field in tbl_metadata called met_ID to associate the metadata with the corresponding page. Obviously, there are lots of types of metadata (keywords, description, language, ROBOTS etc all for SEO purposes) so each type gets a record of its own, which means that each page has about 7 records in tbl_metadata corresponding to it.

 

I'm not sure this is the best way to achieve this, but I couldn't come up with a better solution so I went with it!

 

My problem comes when adding/updating the contents of tbl_Metadata. I have a simple query that retrieves all records associated with a certain page ID. However, I then need to loop through these remaining records and assign the contents of 'met_Content' (where the actual metadata is stored) to local variables.

I'm hoping to achieve something like this: $keywords = 'blah, blah, blah', $description = 'this is a description' and so on and so forth.

 

I'm stuck as to how to do this!

 

Please help!

Link to comment
https://forums.phpfreaks.com/topic/207829-loop-through-recordset/
Share on other sites

Sorry I wasn't thinking when I posted this. What you needed to do is grab the data by the rows but pulling each seperate column out while you do it. What you do is store each column in it's own array and then the different row is the array number. so lets say the ID is 2. Then you use the $keywords["2"] array to get the data from that row and for the column of keywords.

 

<?php
// Make a MySQL Connection
mysql_connect("mysql host", "username", "password") or die(mysql_error());
mysql_select_db("database") or die(mysql_error());

// Get all the data from the "example" table
$result = mysql_query("SELECT * FROM tbl_Metadata") 
or die(mysql_error());  

// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
$id=$row["met_ID"]
$keywords[$id]=$row["met_keywords"];
$description[$id]=$row["met_description"];
$language[$id]=$row["met_language"];
$ROBOTS[$id]=$row["met_ROBOTS"];
} 
?>

Thanks for the reply, the problem is that there aren't unique fields for each type of metadata (keywords, description etc). I have a field called met_Type and another called met_Content and there is a record for each type of metadata. Sorry is that doesn't make too much sense. Here is an e.g:

 

| ------------|-------------------|--------------------------|-----------------------------|

|met_ID    |met_PageID    | met_Type                | met_Content              |

| ------------|-------------------|--------------------------|-----------------------------|

|1              | 1                    | keywords                |blah, blah, blah            |

|2              | 1                    | description              |description here....      |

| ------------|-------------------|--------------------------|-----------------------------|

 

So I have about 7 records in total corresponding to each page, and each record holds one type of metadata.

Ok what you want it to do it first of always use the id for the array like before. You don't have to do any checking for it because it is just assigning that array to that page basically. you just have to have the page call that array.

 

What you need to do is check the met_Type to see what it is. Then based on what it is assign it to separate arrays like I said before. Here is a code you can edit to suit yours.

 

Note: I don't normally use single line if statements so you may have to fix any if statement errors you get. I think I got it right though.

 

<?php
// Make a MySQL Connection
mysql_connect("mysql host", "username", "password") or die(mysql_error());
mysql_select_db("database") or die(mysql_error());

// Get all the data from the "example" table
$result = mysql_query("SELECT * FROM tbl_Metadata") 
or die(mysql_error());  

// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
$id=$row["met_PageID"]
if ($row["met_Type"] = "keywords") $keywords[$id]=$row["met_Content"];
if ($row["met_Type"] = "description") $description[$id]=$row["met_Content"];
if ($row["met_Type"] = "language") $language[$id]=$row["met_Content"];
if ($row["met_Type"] = "ROBOTS") $ROBOTS[$id]=$row["met_Content"];
} 
?>

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.