inVINCEable Posted July 8, 2007 Share Posted July 8, 2007 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 ) ) ) Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted July 8, 2007 Share Posted July 8, 2007 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? Quote Link to comment Share on other sites More sharing options...
inVINCEable Posted July 8, 2007 Author Share Posted July 8, 2007 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. Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted July 8, 2007 Share Posted July 8, 2007 where are these arrays being stored from? (mysql, flatfile) Quote Link to comment Share on other sites More sharing options...
inVINCEable Posted July 8, 2007 Author Share Posted July 8, 2007 I am using CakePHP (a php framework) So right now they are stored in in one of my functions. Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted July 8, 2007 Share Posted July 8, 2007 yeah but how are your voted stored? Do they go into a flatfile or a mysql data base. (If i place votes today how do i retrieve what i placed later on?) Quote Link to comment Share on other sites More sharing options...
inVINCEable Posted July 8, 2007 Author Share Posted July 8, 2007 The votes are stored in a mysql database. This is what grabbed the votes table. Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted July 8, 2007 Share Posted July 8, 2007 so what's your query look like SELECT fields FROM`votes` WHERE user_id = $user_ID) or die(mysql_error()); Quote Link to comment Share on other sites More sharing options...
inVINCEable Posted July 8, 2007 Author Share Posted July 8, 2007 SELECT `Vote`.`id`, `Vote`.`story_id`, `Vote`.`user_id` FROM `votes` AS `Vote` WHERE `Vote`.`story_id` IN (122) Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted July 8, 2007 Share Posted July 8, 2007 why are you grabbed based on story if you are wishing to display by users. Select WHERE User matches instead of story (or both if needed) Quote Link to comment Share on other sites More sharing options...
inVINCEable Posted July 8, 2007 Author Share Posted July 8, 2007 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! Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted July 8, 2007 Share Posted July 8, 2007 well here is my issue you can't tell if a user is voted because you don't have every user ID somewhere you only have the votes that have been casted. Can I see this page in static? Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted July 8, 2007 Share Posted July 8, 2007 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++; } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.