Jump to content

Recommended Posts

        
$save_planet3 = "SELECT saver_id FROM save_planet WHERE saver_id = '".($_SESSION['user_id'])."' 
AND map = 1 ORDER BY time_saved DESC";
$save_planet2 = mysql_query($save_planet3) or trigger_error("SQL", E_USER_ERROR);	

while ($list = mysql_fetch_array($save_planet2)) { 

  $saver_id = $list['saver_id']; 

 

When I echo $saver_id im getting a list of numbers

 

3112  3112  3112  3112 

im trying to do a check if $planet_id is equal to any of these numbers

 

So i figure the only way to do this is to put these numbers in a array with keys first. So I tried to explode it but it doesnt give each one a differant key I noticed.

 

 $check = explode(",", $saver_id);
print_r($check); 

Array ( [0] => 3112 )
Array ( [0] => 3112 )
Array ( [0] => 3112 )
Array ( [0] => 3112 )

 

Once i get this in a workable array i should beable to do this right

 

 

foreach($array as $key => $value) { 

if ($planet_id == $value) { 

//something happends

 

Link to comment
https://forums.phpfreaks.com/topic/255441-searching-through-a-list-of-numbers/
Share on other sites

why not store the result set values into an array inside of the while loop?

 

$arr = array();
while ($list = mysql_fetch_array($save_planet2)) { 

  $saver_id = $list['saver_id']; 
  $arr[] = $saver_id;

}

print_r($arr);

 

Edit: you can also use in_array to check for the value existence.

Thanks alot AyKay47 huge help.

I acctually tried that first but I was using echo $arr; instead of print_r. so i thought it wasnt working lol.

 

That allowed my if statment to end up being true. But I realize now im grabing the wrong thing out of the data base. Now each value is a diffearnt number and its making my if statement false. So im thinking its only reading the first value in the array and thats it. Its not searching through it.

$save_planet3 = "SELECT address FROM save_planet WHERE saver_id = '".($_SESSION['user_id'])."' 
AND map = 1 ORDER BY time_saved DESC";
$save_planet2 = mysql_query($save_planet3) or trigger_error("SQL", E_USER_ERROR);	

             $arr = array();
while ($list = mysql_fetch_array($save_planet2)) { 

            $saver_address = $list['address'];

            $arr[] = $saver_address;		
}
print_r($arr);

$value = (in_array("", $arr)); 

if ($address == $value) {  

 

 

This is something I just posted earlier today, modified to match what you are apparently doing -

 

You would never select a bunch of rows from a table (unless you wanted to display all the matching rows at once from your table) and then use php to loop through them trying to find if a value exists. Php is a relatively slow parsed, tokenized, and interpreted language. The database engine uses compiled code to perform searches and is significantly faster at finding and filtering information than using php code.

 

A generic answer to your question would be to add a term to the WHERE clause in your query to match row(s) that have $planet_id for a value. If you only need to know if a match was found, you can use mysql's COUNT() function in the query or if you actually need the matching row(s) of data, you can use use mysql_num_rows() to find how many row(s) are in the result set.

Ah PFMaBiSmAd Thank you!!!!!! You're awesome!

I didnt even consider doing it that way. I got it working now.

what i have going on here is a loop inside of a loop so it was kinda complex

$address was a changing variable while it was seraching through the array I had.

 

I had to make another query though cause I was using the same query off something else

$save_planet3 = "SELECT address FROM save_planet WHERE saver_id = '".($_SESSION['user_id'])."' 
AND map = 1 AND address= '".($address)."'";
$save_planet2 = mysql_query($save_planet3) or trigger_error("SQL", E_USER_ERROR);	
while ($list = mysql_fetch_array($save_planet2)) { 

$test = $list['address'];
}

if ($address == $test) {

 

so even if i had to make another query using this approach is still faster?

for future reference though id like to know what im doing wrong with searching through my array.

do i have to use "for" loop to do that?

For the code in your last post, you will know that any rows the query matched will have address = '$address'. You don't need further code to test if this is so (you do need code to test if the query matched any rows.) Zero matching rows means $address was not found for that user_id. One or more matching rows means $address was found for that user_id.

 

In your previous code, the $value = (in_array("", $arr)); statement makes no sense. in_array tests if the first parameter is found in the $arr and returns a bool true/false. In that statement, if there was an element with an empty string for a value in the array, $value would be a TRUE, otherwise $value will be a FALSE. You cannot then test if $address == $value because they don't contain the same type of values. The correct logic, assuming you had an actual reason to do it this way, instead of directly in a query, would have been if(in_array($address,$arr)){

 

 

Thanks alot AyKay47 huge help.

I acctually tried that first but I was using echo $arr; instead of print_r. so i thought it wasnt working lol.

 

That allowed my if statment to end up being true. But I realize now im grabing the wrong thing out of the data base. Now each value is a diffearnt number and its making my if statement false. So im thinking its only reading the first value in the array and thats it. Its not searching through it.

$save_planet3 = "SELECT address FROM save_planet WHERE saver_id = '".($_SESSION['user_id'])."' 
AND map = 1 ORDER BY time_saved DESC";
$save_planet2 = mysql_query($save_planet3) or trigger_error("SQL", E_USER_ERROR);	

             $arr = array();
while ($list = mysql_fetch_array($save_planet2)) { 

            $saver_address = $list['address'];

            $arr[] = $saver_address;		
}
print_r($arr);

$value = (in_array("", $arr)); 

if ($address == $value) {  

 

 

I was not exactly sure of your logic for storing all of the database values into an array and them comparing them to a value, so I did not want to simply suggest that you add an additional where clause to your query, which is of course the most efficient suggestion if you are aiming to filter your results based on the value that you are comparing the result set to.

 

what i have going on here is a loop inside of a loop so it was kinda complex

you should almost never have a loop within another loop, especially involving queries, is this still the case with the updated code?

no Sweat AyKay47

 

yah the new code im using now is just one loop now. Then using the addional where statement in the query

 

I understand this now. the first value is what im looking for and the 2nd is the array.

 

so when I did " " that means I was looking for a space with in the array. which their was none so it was staying false.

 

I'm so glad I I understand this now>

 

thanks alot guys

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.