Jump to content

Stuck, trying to compare array values to a value, then create boolean array


inVINCEable

Recommended Posts

I have been trying to figure this out for the past 2 hours and to no avail. :/ So any advice is GREATLY appreciated.

 

Ok, I have an array that returns data like this (see array below) The array[0] contains all the votes for one story as well as information that pertains to that vote, and the array[1] contains vote information for another story. What I need to do is check is a user_id stored in a session, is in the array, and then create a boolean array from that. For example, here I want to check if the user_id is featured in the first set of data, let's say my user_id is 74. Ok, so it is in the first set of data, so we set the boolean variable to true. Next I need to search the second data field for my user_id, 73. It is not featured here so the value in my array is set to 0. I need an array that returns values either 1, for true, your user_id was found in the table, or false, for your user_id was not featured in the table. They need to be in order of the story too. I'm having trouble one comparing the two variables against eachother, since the array is very deep, and two, creating the array on the fly. Any suggestions are recieved with open arms! Thanks for looking !

 

 

 

 

 

 

 

Array

(

    [0] => Array

        (

            [0] => Array

                (

                    [id] => 5

                    [story_id] => 122

                    [user_id] => 74

                    [story] => storyone

                       

 

                )

 

            [1] => Array

                (

                    [id] => 6

                    [story_id] => 122

                    [user_id] => 74

                    [story] => storyone

                     

                )

 

        )

 

    [1] => Array

        (

            [0] => Array

                (

                    [id] => 7

                    [story_id] => 123

                    [user_id] => 45

                    [story] => storythree

                     

 

                )

 

        )

 

)

Link to comment
Share on other sites

okay first lets talk about where this data comes from, secondly your array is difficult to understand is it like this

 

<?php
$data['id']
$data['id']['user_id']
$data['id']['story_id']
$data['id']['story'];
?>

for each ID? and then if so what am i comparing to what?

Link to comment
Share on other sites

cooldude: Each array of data you see there is the data for a vote that was made on the story. So for example, we see the first story has two votes, with the information about those two votes. The second story has one vote, and the information about that vote. What I am trying to do, is search the first story for a user_id that is stored in the session, and if it matches a user_id of a vote in the first story array, then in my boolean array it should set 1 for the first key. Next, we search the second story array and all of its votes to see if the user_id we tested for first (stored in the session) matches any of the votes made in that array. If so, we set the next value in the boolean array to '0' for false, you have no yet made a vote.

Link to comment
Share on other sites

Ok, I have an algorithm that returns the top ten most popular stories. Bam there is my array of the stories, then from that array of the ten popular stories I grab what you see here, the voting records for the ten stories that will be displayed. I want to iterate through these to get an array of data so I can pass that on to </div> element in a foreach loop, like so <div class="<?php $userVoted ? 'userhasvotedcssfile : 'usernotvotedcssfile';?>"

 

I hope that helps you understand why I would like a boolean array returned. Thanks for your time too!

Link to comment
Share on other sites

this will get you the arrays sorted by story and i think you can nav from there:

<?php
$q = "SELECT `Vote`.`id`, `Vote`.`story_id`, `Vote`.`user_id` FROM `votes` AS `Vote` WHERE `Vote`.`story_id` IN (122)";
$result = mysql_query($q) or die(mysql_error());
$i = 0;
while($row= mysql_fetch_array($reult)){
$data[$i][$row['story_id']]['user'] = $row['user_id'];  //I'm assuming only 1 vote per user will show up per story
$data[$i][$row['story_id']]['Vote'] = $row['Vote'];
$i++;
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.