Jump to content

Recommended Posts

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

 

 

Link to comment
https://forums.phpfreaks.com/topic/142315-find-value-in-associative-array/
Share on other sites

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.

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.

 

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);

?>

 

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.

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. :P

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.