remove last id session - php

On my page, I have a checkbox, the user can select checbox and go after in comparison page
For that I use the session save the id checkbox selected.
The user can return on a page wehre there is again checkbox and select other.
The problem with this selections, we can have the same id products selected under a session.
ex
first action
id = 1
id = 2
id = 3
go to selected comparison page
second action
id = 2
id = 4
id = 5
third action
id = 2
id = 4
id = 6
To remove the id I use this code :
$products_id = HTML::sanitize($_POST['products_id']);
if (is_array($_SESSION['productsCompare']) && isset($products_id)) {
$remove_array = $_SESSION['productsCompare'];
$i = 0;
foreach ($remove_array as $value) {
if ($value == $products_id) {
unset($_SESSION['productsCompare'][$i]);
}
$i++;
}
if (count($remove_array) == 1) {
unset($_SESSION['productsCompare']);
}
}
like you can see, if $value == $products_id
the id s removed, but not the id duplicated.
My problem is when there is just one product with the 3 or 4 same duplicate id.
I must click on remove button 4 times to remove the products.
How to udate the loop to erase all the same id in only one time ?
Thank you.
example of $_SESSION['productsCompare'] duplicated
array(11) { [0]=> string(2) "14" [1]=> string(2) "14" [2]=> string(2) "14" [3]=> string(2) "14" [4]=> string(2) "13" [5]=> string(2) "13" [6]=> string(2) "14" [7]=> string(2) "13" [8]=> string(2) "14" [9]=> string(2) "13" [10]=> string(2) "11" }

If you want to remove duplicate values in the array, try using the array_unique method.
also, $_POST['products_id'] might be returning an array of product ids.
if this is the case, then $value == $products_id wont work, and you will have to replace it with in_array($value,$products_id) instead.

Related

PHP mysql session array

I have a little question.
I have few stored values in session Array. These values are ID's of products.
After that i want to display products from my database, but this is not working propertly for me.
Can someone help me a little? :) (I am still learning :) )
<?php
include 'includes/dbconnect.php';
$orderid = $_SESSION['order'];
foreach ($orderid as $value) {
$sql="SELECT * FROM product WHERE productID LIKE '%$value%'";
$result=$conn->query($sql);
while($row=$result->fetch_assoc()){
echo '<tr>';
echo '<td>'.$row["tag"].'</td>';
echo '<td>'.$row["price"].',- Kč</td>';
echo '<td><img src="images/'.$row["tag"].'.jpg" width=70"></td>';
echo '<td>1</td>' ;
echo '<td>X</td>';
}
}
?>
var_dump($orderid); shows:
array(1) {
["order"]=> array(10) {
[0]=> string(2) "44"
[1]=> string(2) "46"
[2]=> string(2) "44"
[3]=> string(2) "54"
[4]=> string(1) "1"
[5]=> string(2) "44"
[6]=> string(1) "1"
[7]=> string(2) "44"
[8]=> string(2) "47"
[9]=> string(2) "74"
}
}
Just for the purposes of SO, I'll make my comment as an answer:
In the $sql query instead of using LIKE '%$value%'"; use this: LIKE '". $value ."'";
This ensures that we actually get the value of the variable.

Echo Specific Data from an Associative Array?

This is for a recipe project where users use a form and enter their own recipes. I have that whole part working. Now I'm trying to retrieve the recipe's information from the database and display it on a page. Anyway, this is the code I have right now.
<?php
include_once('includes/connection.php');
include_once('includes/recipe.php');
$recipe = new Recipe;
if (isset($_GET['id'])) {
$id = $_GET['id'];
$basic_data = $recipe->fetch_data($id);
$sql1 = $pdo->prepare("SELECT recipes.*, categories.* FROM recipes
INNER JOIN categories
ON (recipes.category_ID = categories.category_ID)
WHERE recipes.recipe_id = ?");
$sql1->bindValue(1, $id);
$sql1->execute();
$results1 = $sql1->execute();
echo $results1 = $sql1->fetchAll(PDO::FETCH_ASSOC);
var_dump($results1);
?>
This is what I see on my webpage when this code runs. I've never worked with associative arrays before, so I'm really lost as to how I can echo specific data contained in one. For the purpose of this question, I'm focusing on the "category_name" and I'm trying to echo just the value stored in the "category_name" for this recipe, which happens to be "none". How do I echo just the word "none" to the page?
Arrayarray(1) {
[0]=> array(15) {
["recipe_ID"]=> string(1) "1"
["recipe_name"]=> string(20) "English Muffin Pizza"
["category_ID"]=> string(1) "1"
["servings_ID"]=> string(2) "13"
["prep_hours"]=> string(1) "0"
["prep_minutes"]=> string(2) "20"
["cook_hours"]=> string(1) "0"
["cook_minutes"]=> string(2) "10"
["oven_temp"]=> string(3) "350"
["directions"]=> string(402) "These are the directions."
["extra_comments"]=> string(37) "This is a short extra comment."
["recipe_favorite"]=> string(1) "1"
["recipe_photo"]=> string(0) ""
["created"]=> string(19) "2014-09-20 10:22:39"
["category_name"]=> string(4) "none" }
}
Please let me know if I can give you any more information. It's been a long day and I may be missing something that could help you to help me.
If you are strictly trying to echo category_name in this array you would just write:
echo $results1[0]['category_name'];
You need to use a loop to go through $results. That will echo category_name for all recipes.
foreach ($results1 as $recipe) {
echo $recipe['category_name'];
}
If you know you want the recipe at key 0 (first recipe), you can simply do:
echo $results[0]['category_name'];

Apply foreach to multiple arrays?

I've been playing around with this one for a few days, I'm not a professional coder but I'm trying to put a nice basic system in place to manage checkin/out at a local volunteer rescue organization.
Basically I'm trying to get all members member with the ISLOGGEDIN value set as 1
$loggedin = mysql_query("SELECT * FROM members WHERE ISLOGGEDIN=1");
$loggedinarray=mysql_fetch_array($loggedin);
And present their information (name, etc) into a table:
foreach($loggedinarray as $key=>$value) {
echo "
<tr>
<td>".$value[firstname]." ".$value[lastname]."</td>
<td>".$value[timeloggedin]."</td>
</tr>
";
}
However the results I'm getting are quite random and not what I'm looking for, at all!
var_dump($loggedinarray) output:
array(12) { [0]=> string(5) "5395" ["SES_ID"]=> string(5) "5395" [1]=> string(7) "Anthony" ["FIRST"]=> string(7) "Anthony" [2]=> string(8) "LastName" ["LAST"]=> string(8) "Willison" [3]=> string(1) "1" ["ISLOGGEDIN"]=> string(1) "1" [4]=> string(1) "0" ["TRAINER"]=> string(1) "0" [5]=> string(1) "1" ["OFFICER"]=> string(1) "1" }
Any help would be greatly appreciated!
You have to use while; it was random, because it fetched only one row from table.
$loggedin = mysql_query("SELECT * FROM members WHERE ISLOGGEDIN = 1");
if (mysql_num_rows($loggedin) > 0) {
echo "<table>";
while ($row = mysql_fetch_array($loggedin)) {
echo "
<tr>
<td>".$row['FIRST']." ".$row['LAST']."</td>
<td>".$row['timeloggedin']."</td>
</tr>
";
}
echo "</table>";
}
This code will output every row from table, where ISLOGGEDIN = 1.
PS: about data $row['timeloggedin'], you dont have it in your query (as seen in var_dump), so it will be an empty string.

Grab ID from form array

I believe this is probably a very simple question, but since I'm just dealing with numbers, it sorta makes my brain hurt.
Below is my form with some lines removed due to being irrelevant to the question
$envList = $link->query("SELECT * FROM environments");
echo "<form action = 'editEnvOrderExec.php' method = 'post'>";
echo "<input type = 'hidden' name = 'userID' value = '".$userID."'>"
while($row = $envList->fetch_assoc()){
$eName = $row['name'];
$dID = $row['displayID'];
$eID = $row['id'];
echo $eName.": <input type = 'text' name = 'eID[".$eID."]' value = '".$dID."'><BR>";
}
Upon submitting the $_POST looks like this
array(2) {
["userID"]=>
string(1) "2"
["eID"]=>
array(6) {
[1]=>
string(1) "1"
[3]=>
string(1) "2"
[4]=>
string(1) "3"
[5]=>
string(1) "4"
[6]=>
string(1) "5"
[8]=>
string(1) "6"
}
}
I need to take the values in the brackets, [1][3][4][5][6][8] and for each one have a mysql statement update the displayID with the value [1][2][3][4][5][6]. My problem is I can't figure out how to target each one...
NOTE: I know I need to sanitize values, as well as check if a display ID is already used and such, but this is just v0.1. Trying to get functionality down, then I can tackle those other issues
Easiest way is if you're iterating over the loop using foreach:
$eID = $row['eID'];
foreach ($eID as $key => $value) {
// here $key is the array key you're looking for.
}

PHP Create Query from array, can't pick out both indexes in foreach statement

I am trying to create a query from a form that passes ID and Details fields, the post array looks like this:
array(2) {
["ID"]=> array(9)
{
[0]=> string(3) "10a"
[1]=> string(3) "10b"
[2]=> string(3) "10c"
[3]=> string(3) "10d"
[4]=> string(3) "10e"
[5]=> string(3) "10f"
}
["Details"]=> array(9)
{
[0]=> string(19) "This is textfield 1"
[1]=> string(17) "This is textfield 2"
[2]=> string(0) ""
[3]=> string(0) ""
[4]=> string(0) ""
[5]=> string(0) ""
}
}
The ID is hardcoded into the page and always passed, the details can be filled out by the user as needed.
if ($_POST['OthProb']['Details'] != '') {
// Catch issue text and assign to variable
foreach ( $_POST['OthProb']['Details'] as $key => $value) {
$id = $_POST['OthProb']['ID']->$key;
$dets = $value;
// Focus on fields with values and join the array values together in a query string
if ($dets != ''){
$string = "INSERT into $table (AccountID, OtherID, ID, Details) VALUES ($_POST[AccountID], $_POST[OtherID], '".$id.", '".$dets."')";
$sql = $DBH->prepare($string);
// $sql->execute();
// var_dump ($id);
}
}
}
creates the following
"INSERT into tbl_problems (AccountID, OtherID, ID, Details) VALUES (80, 80, '10f, 'This is Title issue')"
The 'Details' are outputting correctly, but its cycling all the way through the ID's to the last one, is that because I'm nesting a foreach? What would be the best way to structure the loop?
Try to replace this line:
$id = $_POST['OthProb']['ID']->$key;
by
$id = $_POST['OthProb']['ID'][$key];

Categories