I am creating a web site where there are products. I am trying to save the data of completed orders in a cookie, so if the user accessed "previous orders" the data will be retrieved from it.
I tried to write something like this:
function addItemsToCookieOrder($sessionCart,$orderKey)
{
$counter = 0;
foreach($sessionCart as $item)
{
$orderedItem=array(
'product_id'=>$item["p_id"],
'quantity'=>$item['quantity'],
'name'=>$item['name']
);
setcookie("order[$orderKey]
[$counter]",$orderedItem,time()+10000000,"/");
echo $_COOKIE['order'][$orderKey][$counter];
$counter++;
}
}
but I got this errors in the echo line:
Warning: Undefined array key "3ce205a81db8766cd0ffacbd0ac624a4" in C:\xampp\htdocs\WebPHP\includes\functions.inc.php on line 341
Warning: Trying to access array offset on value of type null in
C:\xampp\htdocs\WebPHP\includes\functions.inc.php on line 341
the order key is a random generated key
does any one have a suggestion about how to implement this idea?
Related
I am having an issue with my code - fairly new to PHP so any pointers would be appreciated.
I have the following code
foreach(findCustomAlerts($customerid) as $key=>$value){
echo "ID : ".$rifdid = $value['Catid']."<br>";
echo "Visits required per day : ".$visitsneeded= $value['Visits required']."<br>";
}
foreach(getTodaysVisits($device_id) as $rfid){
foreach($rfid as $key=> $read){
if($key ==$rifdid && $read < $visitsneeded) {
echo"<br>". $key." has not visited enough";
}
}
}
But it is not iterating through the foreach - it is only picking up the first values and then stopping :
ID: 1005
Visits required per day : 3
ID : 2089
Visits required per day : 5
2089 has not visited enough
This is the array that the foreach should be iterating through:
Array
(
[rfid_id] => Array
(
[2089] => 1
[1005] => 3
)
)
I have tried putting a variable in to count the iterations like so :
$i = 0;
foreach(getTodaysVisits($device_id) as $rfid){
foreach($rfid as $key[$i]=> $read){
if($key[$i] ==$rifdid && $read < $visitsneeded) {
echo"<br>". $key[$i]." has not visited enough";
$i++;
}
}
}
But it gives me the following errors :
Warning: Cannot use a scalar value as an array
Notice: Trying to access array offset on value of type int in
So not sure what the best course of action is, any advice greatly appreciated.
This looks like the structure that you are receiving from findCustomAlerts($customerid) in the first for-each loop is not what you are expecting, however the warnings don't give line numbers so I could be wrong.
In your code you have a $key that goes unused, then a $value that you treat as an array.
The warning message you get tells us that you can't use a scalar (or singular) value type as an array. We then get a notice telling us that when we try to use it as an array, either the offset 'Catid' or 'Visits required' is unavailable, likely due to the same reason. I suspect $value is more likely of type integer based on the array example you've shown (1,3).
Run a var_dump() on the output of the findCustomAlerts function and I think you'll understand the issue better.
here is the table I have created in cassandra database
create table followers(
username text,
followedby list<text>,
primary key(username));
using php I have to access the list of followers for a particular username($user="raj"). Below is the query I am using
$result=$session->execute(new Cassandra\SimpleStatement("select followedby from followers where username='$user'"));
I could not figure out how to store the result of the query using php so that all the elements in the list can be accessed one by one i.e. whether to use array or list in php. Below is one of the codes I tried.
foreach($result as $row)
{
$ans=$row['followed'];
echo $ans[0];
echo $ans[1];
}
but the code gave the following error:
Cannot use object of type Cassandra\Collection as array in C:\wamp64\www\LoginRegistrationForm\home.php on line 68
What is the correct method so that I can access all the elements in the list one by one?
foreach($result as $row)
{
foreach ($row['followed'] as $followed) {
echo " {$followed}" . PHP_EOL;
}
}
Driver returns object of type Collection and you are using it as an array.. hence it is giving you error.
Cassandra Collection in PHP
My code:
<?php
$file="./Speed10.txt";
$document=file_get_contents($file);
$rows = explode ('(', $document);
$a[$r][$c];
for($r=0; $r<9103; $r++){ //1903 rows in text file
$a[$r][$c]; // Array declared here - doesnt solve problem
for($c=0; $c<103; $c++){
//$a[$r][$c] = rand();
// print_r($a[$r][$c]);
}
}
foreach ($rows as $ri => $row) {
$a[$ri][$c] = explode (';', $row);
//XXXXXXXXXXXXX
}
print_r($a[1][$c]); // NOT PRINTING*
?>
I have a 2D array as you can see above, it divides a text file into rows and columns.
That part works perfectly, but I try to print out all the cells of one row, it isn't printing.
However, if I move the print_r line to where the X's are, it works (although its being printed out in a loop). Sounds like a scope problem to me but I can't figure out what. I tried initialising the array as a global variable but that didn't fix it.
At the end your script, executed "as is", $c will be 103 but elements in $a[1] will only be set from 0 to 102.
So your accessing a non-existant index, hence it prints nothing.
I have created a slightly modified example which clearly shows the problem.
Notice: Undefined offset: 103 in [...][...] on line 15
if (isset($_GET['addCart'])&& is_numeric($_GET['addCart']))
{
$item = $_GET['addCart'];
$_SESSION ['cart'][$item] = $item;
echo "<h3 class=change> Item ".$item." has been added to cart.</h3>";
foreach($_SESSION['cart'] as $myItem)
{
echo "<br>item: ".$myItem."<br>";
}
}
I can't display the items in the shopping cart.
When I run this code, I get a Warning: Invalid argument supplied for foreach().
EDIT:
However, this works fine:
$_SESSION['shoppingCart'] = array();
$_SESSION['shoppingCart']['1'] = 2;
$_SESSION['shoppingCart']['2'] = 3;
//get all items
foreach($_SESSION['shoppingCart'] as $item)
{
echo"<br>contents of shoppingcart ".$item."<br>";
}
EDIT
Session is started.
I think it has to do with this line:
$_SESSION ['cart'][$item] = $item;
I tried these two and it doesn't work:
`$_SESSION ['cart']['$item'] = $item`;
`$_SESSION ['cart'][] = $item`;
error for first one: Warning: Illegal string offset '$item'
error for second one: Fatal error: [] operator not supported for strings
foreach is used for keyed arrays. If $_SESSION['cart'] is not a keyed array, use a for loop instead.
for ($i=0;$i<count($_SESSION['cart']);$i++){
echo "$_SESSION['cart'][$i]";
}
Well, this depends on how your cart items are stored in $_SESSION['cart']
If $_SESSION['cart'] contains an array of items, you should do something like this:
$items = $_SESSION['cart'];
for($i=0; $i<count($items); $i++)
{
echo $items[$i].'<br />';
}
Your code works fine for me.
Please check if your php session config is ok. Use phpinfo() and check the session.* config, look for session.save_path - check if you have this director and your web server is able to write there.
Also try to put: session_start(); at the beginnig of the script.
You should try initializing the cart index of the session
if(!isset($_SESSION['cart'])) {
$_SESSION['cart'] = array();
}
Before you try to add items to it. Also be sure to start your session using the session_start() function
I used this but my array show blank.
foreach($page_data->result() as $text){ $i++;
echo $name="text".$i; //this line print ok.
$$name=array();
echo $$name['content']=$text->$content; //this line print ok.
print_r($text1);
} print_r($text1);
here i am tying to name the array dynamically as text1,text2,text3.......
but when i print $text1 it shows me a blank array.
can any one help me out with this.
It's simply a syntax ambiguity. $$name['content'] is understood as:
${$name['content']}
I.e. the name of the variable is supposed to be the value of $name['content'], which obviously doesn't exist, which actually leads to an error if you'd enable error reporting. You can solve this with:
${$name}['content'] = $text->$content;
However, you really should solve this by using an array instead of variable variables:
$texts[] = array('content' => $text->$content);
You're incrementing the index before you echo the contents of the variable, so if you only have 1 result, it will try and access an undefined index, you are also re initialising the name array every time you pass through the loop, you should not do this
Results
------------------
Num Content
0 I am a text post
If these were the results returned and you wanted to point to index i, then when your loop goes through this would happen:
i = 0;
i = 1;
assign values to name array
As there is only one result this would then happen
Results
------------------
Num Content
0 I am a text post
- - <----- i = 1 (null pointer exception?)
This would be why no values are showing in your array, try change your code to this:
// Declare i to point at the 0 index
$i = 0;
$name=array();
foreach($page_data->result() as $text){
$name[i]['content']=$text;
$i++;
}
Your name array will no add the text content to a new index with each pass of the loop, i.e. if you had 2 results
$name[0]['content'] = "This is a text post";
$name[1]['content'] = "Here is another post";
Hope this helps.