Getting multiple value from single cookie using php - php

I want to implement shopping cart in php. I am using single cookie to store multiple product id and quantity. Now i want to fetch these values from the cookies.
The data is stored in this format : {Product_id-Quantity}
Like this :
A-2,B-4,C-1,D-3
Now i want to use explode function to get these two values product id and quantity separately.
I do not know how to do this. Can anyone figure out a way to implement this ?

here you go - this saves your cookie-string in a PHP array:
$str = "A-2,B-4,C-1,D-3";
$arr = explode(",", $str);
foreach($arr AS $row)
{
$piece = explode("-", $row);
$cookie[$piece[0]] = $piece[1];
}
var_dump($cookie);
notice: your names and values must not contain , or - because you use them as separators.

Related

How to display data without redundancy

i want to display in my system just one record with the same id.
for example: 01,01,02,02,03,03.
I just want to display 01,02,03.
The array_unique() function removes duplicate values from an array. If two or more array values are the same, the first appearance will be kept and the other will be removed.
$number = "01,01,02,02,03,03";
$result = implode(',',array_unique(explode(',', $number )));
echo $result; // output 01,02,03
Happy Programming

Make HTML list from PHP array?

I'm very new with PHP/Mysql, but I'm trying to make movie database.
There's a form where people can put in info about the movies, and the send button stores it in my database. Then another web page displays the movie list.
The text input and dropdown selections were easy. What I'm really struggling with is the multiple checkbox part. After hours of struggling I finally learned about the php array and how I can store all the values from my checkboxes using implode. My code looks like this:
$genre = implode( ';' , $_POST['genre'] );
This saves all the selected genres in the database, seperated by ;
However, I need help in displaying this data on my html page the way I want:
First off, I want to retrieve the results in a html list, instead of a;b;c
Second, I want to change a;b;c etc to actual words - for example Horror, Action, Comedy. Can this be done?
Hope someone can help! Thanks!
EDIT:
A friend told me that the best way to handle multiple checkbox values is to store them in a different table (moviegenres) and use mapping. So I rearranged my database like this:
One table called 'movies' that has the columns 'movieID' and 'title', and one table called 'moviegenres' that has the columns 'movieID' and 'moviegenre'.
I can still save the title into 'title' in 'movies', but when I want to add a command for adding genre to the 'moviegenres' table, nothing works...
What is the simplest way to do this?
Do I need to different commands for inserting into two tables, or can I do everything in one command, using one variable?
You can take the data say
$genre = 'a:b:c';
and use
$genreArr = explode(';', $genre);
to get this as an array. where you can get the values like
$genreArr[0] = 'a';
$genreArr[1] = 'b';
then in php page, use this code:
echo '<ul>';
foreach($genreArr as $g)
{
echo '<li>'. $g.'</li>';
}
echo '</ul>';
Yes you can used the explode() function to get an array of database checkbox values like you have fetching from db a;b;c
$values = explode(';', $dbcheckboxes);
After this just display in html. If you want to display actual words then make one array of actual words like
$actual_words = array(a => 'Horror', b => 'Action', c => 'Comedy');
Just compare the above database values with actual words array keys and you will get it.
You can get data explode it and loop it to print those values in html.
$checkbox_values=explode(";","a;b;c");
$map=array("a"=>"Apple","b"=>"banana","c"=>"carrot");
foreach($checkbox_values as $value)
{
echo "<p>"+$map[$value]+"</p>";
}
This would be a solution for your question.But since creating a map variable wont be dynamic, it is better to store the display string as it is to database such as "apple;banana;carrot".
$genre = implode( ';' , $_POST['genre'] );
$array_values = explode(';', $genre);
foreach ($array_values as $values) {
echo $values.'<br>';
}

Php - Find only one number in a string

I have a php script that loads different IDs and send them to a page on a GET variable, they are all separated by a coma:
.../page.php?items=12,13,43,17,
I then load a mysql with a while loop so that a table shows up, and I need to see if the id of the table item is the same then it adds a style to that table row.
So what I did was:
$style = (strstr($_GET['ids'], $segn['itemid'])) ? 'style="background:lightgreen;"' : '' ;
<tr class="gradeX" <?= $style ?> >;
In this case it worked for single numbers. But in case i have this IDS: 1, 2, 3 ... 12 and the page get request is "page.php?items=12" I get the ids '12', '1' and '2'.
I thought i could use explode(',' , $_GET['ids']) but then I guess it goes too long.
How do I get those the ids? (I've no affinity whatsoever for regexp...).
You have to work with the ids as an array, not as a string. So make an array first:
$ids = explode(',', $_GET['items']);
And then check if the current item is in the array:
$style = in_array($segn['itemid'], $ids) ? 'style="background:lightgreen;"' : '';
That said, you should be using a CSS class instead of inline styles.
Instead of:
strstr($_GET['ids'], $segn['itemid'])
You could do this:
in_array($segn['itemid'], explode(',',$_GET['ids']));
You don't need regex for this.
Split your get variable using explodes as you did:
$items = explode(",", $_GET['items']);
Be careful- you used the wrong name in your code (your get variable is "items", not "ids");
and then use in_array to check to see if it's in there.
$style = in_array($segn['itemid'], $items) ? 'style="background:lightgreen;"' : '';

How do I split a cookie using php

I have a cookie which I'm trying to split. The cookie is in this format:
key = val1,val2,val3 (where each value is separated by commas)
is there a way for me to split this in a loop so that I can directly access val3?
I've tried using the explode() function with no success.
for ($i = 0; $i < count($_COOKIE); $i++)
{
$ind = key($_COOKIE);
$data = $_COOKIE[$ind];
//I try and slit the cookie here
$cookie_temp = explode(",",$_COOKIE[$ind]);
//Here is where I wanted to display Val3 from the cookie
print $cookie_temp[2];
next($_COOKIE);
}
my code works fine but I then end up with all my Val3 in a large array. For example, my val3's are numbers and they get put in an array. Can I split this even further?
First of all, I'm hoping you know the name of the cookie you're trying to get the value of. Let's call it mycookie in the rest of my answer.
Second, just scrap the whole loop thing and just access $_COOKIE['mycookie'] directly.
Then, you can now call explode(",",$_COOKIE['mycookie']) to get the separate values.
Next, just get index 2 with [2] as you are in your current code.
As a shortcut, if the second one is the only one you need:
list(,,$val) = explode(",",$_COOKIE['mycookie']);
Assuming you are looping because you have multiple comma-separated cookie key/value groups, use a foreach() instead and with list() you can retrieve the third value with a direct assignment.
foreach ($_COOKIE as $key=>$value) {
list($v1, $v2, $v3) = explode("," $value);
echo $v3;
}
If you have only one cookie value to access, there is no need for the loop, and you can directly call explode(",", $_COOKIE['key'])
PHP 5.4 allows array dereferencing, whereby you can directly access the array element off of the explode() call, but this won't work in earlier PHP versions.
echo explode(",", $_COOKIE['key'])[2];

Explode the results to add a nested tree to a database in PHP and order it

I'm using a nestedsortable jQuery plugin that gives me the order/sort of the elements serialized.
And example of this serialitzation (root means parent_id=0):
id[1]=root&id[5]=1&id[2]=1&id[3]=1&id[4]=3
First thing I'll do is explode by &:
$serialized = "id[1]=root&id[5]=1&id[2]=1&id[3]=1&id[4]=3";
$exploded = explode("&", $serialized);
But I don't know then how to manage a id[1]=root or id[3]=1. How I can do it?
And another question. In this way I don't know which is how to store the order. When I've the exploded with in array like array("id"=>1, "parent"=>"root"); I've to store the order. I will do it with an index, but how I recognise nested levels?
An example:
$i = 0;
foreach($exploded as $explode)
{
//update every id in MySQL and set parent=$explode["parent"] and order=$i
$i++;
}
But if I've N levels, how I can have a index $i for every one of them?
Thank you in advance!
Rather than exploding, you could try parse_str()
<?php
parse_str("id[1]=root&id[5]=1&id[2]=1&id[3]=1&id[4]=3",$result);
print_r($result);
?>
From there you can work with the array.

Categories