tgavin Posted October 8, 2009 Share Posted October 8, 2009 Here's my array. Array ( [0] => 90976 [1] => 90977 [2] => 90978 [3] => 90979 [4] => 90980 [5] => 90981 [6] => 90982 [7] => 90983 [8] => 90984 [9] => 90985 [10] => 90986 [11] => 90987 [12] => 90988 ) I'm trying to do a pagination script for a results page, not a listing page. Like in a photo gallery script. I'm not browsing the thumbnails, I'm looking at the large image. I have the script working to the point that if you start at the beginning, you can page through to the end and back to the beginning, in order. The problem I have is, what if you start in the middle? I can't tell where I am in the array. How do I match the ID number to the position in the array? Quote Link to comment https://forums.phpfreaks.com/topic/176998-finding-where-i-am-in-the-array/ Share on other sites More sharing options...
mikesta707 Posted October 8, 2009 Share Posted October 8, 2009 $key = array_search($array, "Value Im Looking For"); $key would be the key of the array with that value. assuming all entries in the array are different, this should suit your needs. if you need to get multiple keys that might have to same value, use the array_keys function with and use the optional third search parameter. check out the manual for more information Quote Link to comment https://forums.phpfreaks.com/topic/176998-finding-where-i-am-in-the-array/#findComment-933232 Share on other sites More sharing options...
tgavin Posted October 8, 2009 Author Share Posted October 8, 2009 Excellent! Thank you! I've been working on this for two days and am FINALLY making some progress thanks to array_search(). I've been trying current(), next(), and everything else. I'm trying to get the ID from the array. When the script is first called, the ID is retrieved via $_GET['id']. After that I need to search the array for the position I'm in ($_GET['pos'] is passed on every page) and the corresponding ID number. This is what I have so far. // get the ID of every episode in this series/season $query_episodes_count = "SELECT id FROM movies WHERE kind='TV Show' AND show_name='".$_GET['show']."' AND season='".$_GET['season']."' ORDER BY episode ASC"; $sql_episodes_count = mysql_query($query_episodes_count) or die(mysql_error()); // if episodes exist if(mysql_num_rows($sql_episodes_count) > 0) { // build an array of the ID numbers $episodes_array = array(); while($row_episodes_count = mysql_fetch_array($sql_episodes_count)) { array_push($episodes_array,$row_episodes_count['id']); } // get the position in the array (which episode are we browsing?) $pos = abs((int) $_GET['pos']); // get the current show ID number in the array $id = array_search($_GET['id'],$episodes_array); // if $_GET['id'] isn't available, how do I get the current show's ID number from the array? // get the current position ($pos) and then match it to an ID number in the array // get the show's data $query = "SELECT * FROM movies WHERE id=$id)"; $sql = mysql_query($query) or die(mysql_error()); $row = mysql_fetch_assoc($sql); echo $row['title'].'<br />'; // pagination if($pos > 0) { // show prev link $pos_prev = '<div style="float:left; padding-right:10px;"><a href="pagtest.php?pos='; $pos_prev .= $pos-1; $pos_prev .= '">previous</a></div>'; } if($pos < count($episodes_array) - 1) { // show next link $pos_next = '<div style="float:left"><a href="pagtest.php?pos='; $pos_next .= $pos+1; $pos_next .= '">next</a></div>'; } echo $pos_prev; echo $pos_next; } Quote Link to comment https://forums.phpfreaks.com/topic/176998-finding-where-i-am-in-the-array/#findComment-933244 Share on other sites More sharing options...
mikesta707 Posted October 8, 2009 Share Posted October 8, 2009 $id = array_search($_GET['id'],$episodes_array); this won't get the ID number. $_GET['id'] is the ID number, $id would be the key at that number. you could access it via $num = $episodes_array[$id]; but that is kind of pointless because the value that would get is the value of $_GET['id'] and what is $pos? how are position and ID different? i know id is the id number of the show. what is pos in relation to the show? does it have any relation? and if you have purely php code, use php tags, not code tags. it makes it nicer to read nvm you did... hmm i must be hallucinating again... ignore that last line Quote Link to comment https://forums.phpfreaks.com/topic/176998-finding-where-i-am-in-the-array/#findComment-933246 Share on other sites More sharing options...
tgavin Posted October 8, 2009 Author Share Posted October 8, 2009 $_GET['id'] is only passed on the first page. Using the next and prev links won't pass $id, however, they do pass pos, which is short for position and tells the script which position in the array to go to, &pos=0, &pos=1, etc. The problem I've been having for the past couple of days is that, every time I go to a new page, the array resets and I start from 0, which, in the array = 90976. So I've been trying to match $pos with the key. Using the array, if I'm on page 5, then &pos=4 and I want to find the ID number that has 4 as the key, which would be 90980. Then I could do a sql query using that ID number. Yes, it would be a LOT easier to pass &id= in the URL. I tried that using next() and received errors. I can't figure out how to get the next or previous IDs in the array, so I started doing it this way. At this point I'm completely wiped out and somewhat confused because I've been all over the place with this. I'm trying to keep it simple, but fear I'm making this overly complicated. Quote Link to comment https://forums.phpfreaks.com/topic/176998-finding-where-i-am-in-the-array/#findComment-933267 Share on other sites More sharing options...
mrMarcus Posted October 8, 2009 Share Posted October 8, 2009 if you are not passing a start position (either by $_GET, $_SESSION, etc.), then you're array will always reset itself because if it is not given a specific starting point, it's just going to start at the beginning of the index. why not continue to pass values using $_GET? reconsider your plan of attack before moving on, otherwise, you could be headed for many headaches. Quote Link to comment https://forums.phpfreaks.com/topic/176998-finding-where-i-am-in-the-array/#findComment-933275 Share on other sites More sharing options...
mikesta707 Posted October 8, 2009 Share Posted October 8, 2009 if you are passing the key of the array as the get variable pos than use it... $id = myarray[$pos]; Quote Link to comment https://forums.phpfreaks.com/topic/176998-finding-where-i-am-in-the-array/#findComment-933278 Share on other sites More sharing options...
tgavin Posted October 8, 2009 Author Share Posted October 8, 2009 Nothing would make me happier than passing the previous or next ID in the URL, or using SESSION. That would make my life a lot easier. My last two days have been spent trying to find my position in the array. I just don't know how to do it. Trying to get the next or previous ID numbers hasn't worked for me. I wasn't passing the key in the get variable, I just realized that pos and the key were the same, so I was trying to us it as a place holder. How do I get the next and previous ID numbers so I can pass them in the URL? or at the lease just find my place in the array (using SESSION, or whatever)? Thanks for your help! Quote Link to comment https://forums.phpfreaks.com/topic/176998-finding-where-i-am-in-the-array/#findComment-933290 Share on other sites More sharing options...
mikesta707 Posted October 8, 2009 Share Posted October 8, 2009 what is pos... exactly. what does it represent. you are not being clear on what exactly pos is. from the looks of your code, pos seems to be the key in the array of what episode id you are watching Quote Link to comment https://forums.phpfreaks.com/topic/176998-finding-where-i-am-in-the-array/#findComment-933295 Share on other sites More sharing options...
mrMarcus Posted October 8, 2009 Share Posted October 8, 2009 using what 'mikesta707' has, just pass a value in the URL like so .. 0 would be the first page, 1 would be the second, and so on .. ?pos=1 (to go to the second page/picture); now, use: $pos = clean_me ($_GET['pos']); //remove clean_me() if you do not plan on sanitizing; $id = myarray[$pos]; boom, $id no equals the ID number .. in this case, if ?pos=1, then going back to your original array up top, $id would be '90977'; hope this helps. Quote Link to comment https://forums.phpfreaks.com/topic/176998-finding-where-i-am-in-the-array/#findComment-933296 Share on other sites More sharing options...
tgavin Posted October 8, 2009 Author Share Posted October 8, 2009 mrMarkus, that's already being done in the pagination links. That's what pos is, the position within the array. My problem has been matching pos to the ID number in the array. What I'm trying to do is (using psudo code) // get the position number $pos = $_GET['pos']; // get the corresponding ID number in the array $id = $episodes_array[$pos]['id'] // query using the ID number $sql = mysql_query("SELECT * FROM movies WHERE id=$id"); I'm not trying to make this complicated. I apologize if it's confusing. It seems a simple matter of matching pos to the ID and then performing a query using the result. Quote Link to comment https://forums.phpfreaks.com/topic/176998-finding-where-i-am-in-the-array/#findComment-933305 Share on other sites More sharing options...
mikesta707 Posted October 8, 2009 Share Posted October 8, 2009 from your first post, $episodes_array isn't multidimensional. change $id = $episodes_array[$pos]['id']; to $id = $episodes_array[$pos]; and see what happens Quote Link to comment https://forums.phpfreaks.com/topic/176998-finding-where-i-am-in-the-array/#findComment-933312 Share on other sites More sharing options...
mrMarcus Posted October 8, 2009 Share Posted October 8, 2009 what number exactly are you trying to get to use for the db? 'cause the code i gave you will grab the value from the array by using the key that is $pos; this really isn't a difficult thing, i'm sure of it, i just don't know that i'm following you 100% .. can you give a better example/scenario as to what you're doing and how? Quote Link to comment https://forums.phpfreaks.com/topic/176998-finding-where-i-am-in-the-array/#findComment-933315 Share on other sites More sharing options...
tgavin Posted October 8, 2009 Author Share Posted October 8, 2009 I was screwing around with that earlier. I was also using while($row_episodes_count = mysql_fetch_array($sql_episodes_count)) { $episodes_array[] = $row_episodes_count; } both of which gave me a multidimensional array. I still couldn't figure out how to match pos with the ID Quote Link to comment https://forums.phpfreaks.com/topic/176998-finding-where-i-am-in-the-array/#findComment-933316 Share on other sites More sharing options...
tgavin Posted October 8, 2009 Author Share Posted October 8, 2009 I have to run out for an appointment. I'll bbl in a couple of hours and try your code mrMarcus. Again, thanks for your help! Quote Link to comment https://forums.phpfreaks.com/topic/176998-finding-where-i-am-in-the-array/#findComment-933317 Share on other sites More sharing options...
tgavin Posted October 8, 2009 Author Share Posted October 8, 2009 Let's see if this helps. The code has been commented more thoroughly. // get the ID of every episode in this series/season so we can build an array of ID numbers $query_episodes_count = "SELECT id FROM movies WHERE kind='TV Show' AND show_name_slug='".escape('all-in-the-family')."' AND season='".escape(1)."' ORDER BY episode ASC"; $sql_episodes_count = mysql_query($query_episodes_count) or die(mysql_error()); // if episodes exist if(mysql_num_rows($sql_episodes_count) > 0) { // Since we may not be starting at the first episode (position 0 in the array) // we need to get the position in the array so we know which episode ID to look up // pos gives us this position and is passed in the pagination links, built at the bottom of the script // pos is always available $pos = abs((int) $_GET['pos']); // build an array of the episode ID numbers $episodes_array = array(); while($row_episodes_count = mysql_fetch_array($sql_episodes_count)) { //array_push($episodes_array,$row_episodes_count['id']); $episodes_array[] = $row_episodes_count; } // $_GET['id'] is ONLY passed during the FIRST visit to this page // after that we need to get the episode ID from $episodes_array with a key that matches $pos // so we don't start from the beginning of the array if(isset($_GET['id'])) { $id = $_GET['id']; } else { // get the ID from the array that correlates to the key matching $pos //$id = array_search('id',$episodes_array); $id = $episodes_array[$pos]; } // get the show's data so we can display it on the page $query = mysql_query("SELECT * FROM movies WHERE id=$id") or die(mysql_error()); $row = mysql_fetch_assoc($sql); // print the show's information echo $row['title'].'<br />'; // build the pagination if($pos > 0) { // show prev link $pos_prev = '<div style="float:left; padding-right:10px;"><a href="pagtest.php?pos='; $pos_prev .= $pos-1; $pos_prev .= '">previous</a></div>'; } if($pos < count($episodes_array) - 1) { // show next link $pos_next = '<div style="float:left"><a href="pagtest.php?pos='; $pos_next .= $pos+1; // can we put in the next ID? //$pos_next .= '&id='.next($episodes_array); $pos_next .= '">next</a></div>'; } echo $pos_prev; echo $pos_next; } Quote Link to comment https://forums.phpfreaks.com/topic/176998-finding-where-i-am-in-the-array/#findComment-933391 Share on other sites More sharing options...
mikesta707 Posted October 8, 2009 Share Posted October 8, 2009 do a print_r on $episodes_array and post the output Quote Link to comment https://forums.phpfreaks.com/topic/176998-finding-where-i-am-in-the-array/#findComment-933392 Share on other sites More sharing options...
tgavin Posted October 8, 2009 Author Share Posted October 8, 2009 Array ( [0] => Array ( [0] => 90976 [id] => 90976 ) [1] => Array ( [0] => 90977 [id] => 90977 ) [2] => Array ( [0] => 90978 [id] => 90978 ) [3] => Array ( [0] => 90979 [id] => 90979 ) [4] => Array ( [0] => 90980 [id] => 90980 ) [5] => Array ( [0] => 90981 [id] => 90981 ) [6] => Array ( [0] => 90982 [id] => 90982 ) [7] => Array ( [0] => 90983 [id] => 90983 ) [8] => Array ( [0] => 90984 [id] => 90984 ) [9] => Array ( [0] => 90985 [id] => 90985 ) [10] => Array ( [0] => 90986 [id] => 90986 ) [11] => Array ( [0] => 90987 [id] => 90987 ) [12] => Array ( [0] => 90988 [id] => 90988 ) ) Quote Link to comment https://forums.phpfreaks.com/topic/176998-finding-where-i-am-in-the-array/#findComment-933395 Share on other sites More sharing options...
tgavin Posted October 9, 2009 Author Share Posted October 9, 2009 any progress on this? Quote Link to comment https://forums.phpfreaks.com/topic/176998-finding-where-i-am-in-the-array/#findComment-933884 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.