This is a follow on to a previous question.
I have 4 buttons on my home page, each has their own ID associated to them. When someone clicks a button Jquery grabs the button ID and sends it to PHP via Ajax where it is then added as a session variable. If people click multiple buttons it will send the ID of each button. So in the end it is possible that I could have 4 variables in the array.
The Session is created as an array so that it can hold the multiple ID's: Like so:
<?php
session_start();
if ( ! is_array( $_SESSION['ids'] ) ) {
$_SESSION['ids'] = array();
}
if ( ! in_array($_POST['session'], $_SESSION['ids'] ) ) {
$_SESSION['ids'][] = $_POST['session'];
}
?>
That all works correctly and If i print_r $_SESSION['ids']
it correctly shows the id of each button that was clicked and stored eg:
[0] => 1, null, [1] => 3, null, [2] => 4
(Where the button id's that were stored are 1, 3 and 4.
The issue now is that I need to query the database for the button ID's that have been stored.
How can I break the array up and get just the values eg: The button ID 1, 3
and 4
. So that I can query the database with them?
You can use the implode()
function, described here .
$in = implode(',',$_SESSION['ids']);
mysql_query("SELECT * from yourbd where id in ($in)");//just an eg. mysql_* is deprecated
Look at this function. Good luck!
$buttons=$_SESSION['ids'];
$str=implode(', ',$buttons);
$sqlquery = mysql_query("Select * from table where btn_id IN ($str)");
foreach ($IdsArray as $myId) {
$newIds[] = 'my_id = ' . $myId;
}
$orSeparatedIds = implode(" OR ", $newIds);
$query = "select my_id from mytable where $orSeparatedIds";
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.