effex Posted January 25, 2009 Share Posted January 25, 2009 Hello, I am querying a MySQL database and storing the results in an array using the following method: $resource = mysql_query("select * from tImages where imgCategory = '" . $category . "' ORDER BY imgTitle ASC"); $row = mysql_fetch_assoc($resource); I'd like to be able to find out which index/row (not sure of the terminology in this case) a particular value falls on. The table has a column called "imgID", I want to find the "index/key/row" that my variable $imgid matches up on in these results. If that is not possible, I'd like to implement a counter that will count until this value is found in the results. This count / value is needed for later operations in my application. Any help would be appreciated, let me know if you need further clarification. -Mike Quote Link to comment https://forums.phpfreaks.com/topic/142315-find-value-in-associative-array/ Share on other sites More sharing options...
genericnumber1 Posted January 25, 2009 Share Posted January 25, 2009 If you're not going to use any item other than the one for which you're searching, you should just specify it in the WHERE clause of your mysql query, it will be quicker than any linear search (which is all you could really do) you could write up in PHP. Quote Link to comment https://forums.phpfreaks.com/topic/142315-find-value-in-associative-array/#findComment-745689 Share on other sites More sharing options...
effex Posted January 25, 2009 Author Share Posted January 25, 2009 If you're not going to use any item other than the one for which you're searching, you should just specify it in the WHERE clause of your mysql query, it will be quicker than any linear search (which is all you could really do) you could write up in PHP. See, the row in the actual table is not what I need, I need to know what "row" it appears on in the dataset. If there was a way to loop through the dataset and increment a counter until it finds the proper row would be great. So, say the table has 100 rows and my query returns 25 rows. I want to find what row value "19" appears on for example... or increment a counter until that point. Quote Link to comment https://forums.phpfreaks.com/topic/142315-find-value-in-associative-array/#findComment-745697 Share on other sites More sharing options...
redarrow Posted January 25, 2009 Share Posted January 25, 2009 would this work, but he will need to add the other database field names he wants to echo out. <?php $resource = mysql_query("select count(imgID) as counted from tImages where imgCategory = '" . $category . "' ORDER BY imgTitle ASC"); $row = mysql_fetch_assoc($resource); ?> Quote Link to comment https://forums.phpfreaks.com/topic/142315-find-value-in-associative-array/#findComment-745703 Share on other sites More sharing options...
effex Posted January 25, 2009 Author Share Posted January 25, 2009 would this work, but he will need to add the other database field names he wants to echo out. <?php $resource = mysql_query("select count(imgID) as counted from tImages where imgCategory = '" . $category . "' ORDER BY imgTitle ASC"); $row = mysql_fetch_assoc($resource); ?> Well, see I'm not trying to count the rows returned. I'm trying to do something along the lines of a do until loop. I have a value of example "45", I want to run through my results until I reach "45" as "imgID". This way I have the row number this fell in in the results. See, PHP is not my strongest language, but in my head it looks something like this... I'm sure this syntax doesn't exist, but work with me, LOL. $i = 0; $rowcounter = 0; do { $i++; $rowcounter++; }until($row[$i]["imgID"] == $imgID); Does that make sense? The variable "$imgID" is something that was passed from a previous page, I just want to know when I run the query I provided, where that would appear in a result set. Quote Link to comment https://forums.phpfreaks.com/topic/142315-find-value-in-associative-array/#findComment-745707 Share on other sites More sharing options...
Philip Posted January 25, 2009 Share Posted January 25, 2009 Something like: <?php // ... code up here for mysql // What Id? $id_to_find = '75'; // set counter $i = 0; while($row = mysql_fetch_assoc($resource)) { if($row['id'] == $id_to_find) { // do some stuff break; } $i++; } echo $i; // row number // OR.... // this should work too... // set id to find $id_to_find = '75'; // set counter $rownum = 0; // run loop for(;$row = mysql_fetch_assoc($resource) && $row['id'] != $id_to_find; $rownum) { } echo $rownum; // row number ?> I think that should work, I'm pretty drowsy from work, so I apologize if it doesn't. In my head it works. Quote Link to comment https://forums.phpfreaks.com/topic/142315-find-value-in-associative-array/#findComment-745736 Share on other sites More sharing options...
corbin Posted January 25, 2009 Share Posted January 25, 2009 Just of curiosity, why would you ever need to know what row it's in? Also, will the other imgids always be smaller? If so: $imgid = x; $q = "SELECT COUNT(imgid) FROM table WHERE imgid <= {$imgid};"; Quote Link to comment https://forums.phpfreaks.com/topic/142315-find-value-in-associative-array/#findComment-745752 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.