Jump to content

Kind of lost on how to apporach this - Array into interger / removing quotes?


Recommended Posts

Well, In my DB i have user items as follows:

13,12,11,9,27,15,16,22,21,23,24,26,29,30,31,32,33

 

Each ID is a user item that has been imploded to be sent to the DB. Well, I', trying to take them out of the DB now, and I'm having a small problem turning them back into integers.  Here's what I've done.

 

$items = explode(",", $row['user_item_id']);

So I get:

  0 => string '13' (length=2)
  1 => string '12' (length=2)
  2 => string '11' (length=2)
  3 => string '9' (length=1)
  4 => string '27' (length=2)
  5 => string '15' (length=2)
  6 => string '16' (length=2)
  7 => string '22' (length=2)
  8 => string '21' (length=2)
  9 => string '23' (length=2)
  10 => string '24' (length=2)
  11 => string '26' (length=2)
  12 => string '29' (length=2)
  13 => string '30' (length=2)
  14 => string '31' (length=2)
  15 => string '32' (length=2)
  16 => string '33' (length=2)

 

Then I do this:

foreach($items as $item){
				var_dump($item);
				$sql = 'SELECT *
				FROM `MYTABLE`
				WHERE item_tab = '.$tab_id.'
				AND item_id = '.$item.'
				AND item_pose =0';
			}

 

and get this:

string '13' (length=2)

string '12' (length=2)

string '11' (length=2)

string '9' (length=1)

string '27' (length=2)

string '15' (length=2)

string '16' (length=2)

string '22' (length=2)

string '21' (length=2)

string '23' (length=2)

string '24' (length=2)

string '26' (length=2)

string '29' (length=2)

string '30' (length=2)

string '31' (length=2)

string '32' (length=2)

string '33' (length=2)

 

Now, this works how I want it to except, I want a integer value not a string. What would be the best way to approach this?

intval

 

You shouldn't loop queries though, unless you absolutely have to.

 

Since you've stored the values in a comma separated list, rather than in their own rows, you will need to use two queries though. Check out the IN syntax

http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_in

intval

 

You shouldn't loop queries though, unless you absolutely have to.

 

Since you've stored the values in a comma separated list, rather than in their own rows, you will need to use two queries though. Check out the IN syntax

http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_in

 

Highly appreciate the MySQL IN syntax. I have never used it before. I'll try these out and update you accordingly.

removing quote

 

There are no actual quotes around the values in the array. The var_dump simply shows them that way to indicate where the string value being displayed starts and stops.

 

There's no need to do anything with the array values to use them. If you were converting to json, you might need perform a type conversion, but it is not needed for what you are showing in this example.

If you had stored them in their own rows, you could accomplish everything in a SINGLE query. This is the way RDBMS were designed to work :D

 

SELECT * FROM `MYTABLE` WHERE `item_id` IN (
# Your first query would go here
SELECT `id` FROM `itemstable` WHERE `something` = 'foobar'
) AND `item_tab` = '$tab_id'

 

Saves you for having to verify the results of two queries, and is slightly faster :)

 

removing quote

 

There are no actual quotes around the values in the array. The var_dump simply shows them that way to indicate where the string value being displayed starts and stops.

 

There's no need to do anything with the array values to use them. If you were converting to json, you might need perform a type conversion, but it is not needed for what you are showing in this example.

 

He didn't say anything about quotes, just that he wanted to values to be integers. Though, in PHP, there isn't a big difference (though a few functions are specific about argument types).

 

[edit] thread title [/edit]

I got it working correctly now! For someone reason, the array didn't have the right keys..anyway,

$sql = 'select * from MYTABLE where item_id IN ('.$items.') AND item_tab = "'.$tab_id.'" AND `item_pose` = 0';

Worked fine!

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.