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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Serialize problem 1

Status
Not open for further replies.

webdev007

Programmer
Sep 9, 2005
168
What I need to achieve:
Out of an Ajax cascade of DD boxes (php MySQL driven) a user may select a unique tag
And figure if in a given category something similar exits
The existing tags are stored in my table as serialized array
Ex: a:3:{i:0;s:6:"asasas";i:1;s:4:"dddd";i:2;s:10:"ddddd mmmm";}

The user’s selected tag is named: $tags_selected and passed as GET as well as a head_category

First I select all existing tags within the same category

$result = $db->query("
SELECT
quest, tags
FROM
quest
WHERE
head_category_quest='$head_category_quest'
");

Then I unserialize stored tags
And need to grab a corresponding row value (for example: id)
But I cannot figure how doing the sub query
By matching one element of the unserialized
With the single passed value from ajax.
The following is delivering some odds result

How may I loop through an unserialize array and match any of its value with the single value (passed by GET) thus being able to read the whole row and getting some values out of it?

while ($row=$db->fetch_array($result) )
{
$tags=$row['tags'];
$tags=unserialize($tags);print_r($tags);
if(in_array($tags_selected, $tags))
{
$tags_selected=serialize($tags_selected);//print_r($tags_selected);

$result2 = $db->query("
SELECT
quest, tags
FROM
quest
WHERE
head_category_quest='$head_category_quest'
AND
tags='$tags_selected'
");
while ($row=$db->fetch_array($result2) )
{
$quest=$row['quest'];
echo"AA $quest<br>";
}

}
}
 
In your example, your serialized array, when unserialized, is:
Code:
Array
(
    [0] => asasas
    [1] => dddd
    [2] => ddddd mmmm
)
If I understand your question correctly, it should just be a matter of knowing which array element to go after.

However, if dddd is one of your tags names that you are trying to match, you may be better off with the following code, hence uncomplicating the situation:
Code:
$sql = "SELECT quest, tags FROM quest WHERE "
     . "head_category_quest='$head_category_quest' "
     . "tags LIKE '%{$tags_selected}%'" ;

$result = @$db->query($sql) ;
if ($result && mysql_num_rows($result) > 0) {
  while ($row=$db->fetch_array($result) ) {
    echo "AA {$row['quest']} <br/>" ;
  }
} else {
  // handle errors / no records here
}
// ... more code here ...
If I didn't interpret your intent correctly here, please post some of your actual code so we may better get an understanding of what you're trying to accomplish.


Greg
 
grtammi, looks like you are spot on!
yes, this will uncomplicate the situation
and it answers my quest
thank you very much
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top