PHP - Session array - prevent duplicate entrys - php

This one is really bugging me, and can not find a easy solution.
On a detail view of a product, i set the info to a session, maximum 4:
$_SESSION['recent'][] = array(
'id' => $productimgfolder,
'title' => $product['Product']['title'],
'link' => $_SERVER['REQUEST_URI'],
'image' => 'img/products/'.$productimgfolder.'/'.$product['Product']['mainpicture']
);
$_SESSION['recent'] = array_slice($_SESSION['recent'],-4);
This part works, if i output the session:
edit image => this is wat happens if i reload the detail view
The part i'am struggling with is, when i reload a detail view, the info in the session is duplicated.
How can i prevent this from happening?
I tried it with in_array & array_unique, i'am doing something wrong

The easy solution:
if the id is unique, you can do like this:
if(!array_key_exists ($productimgfolder, $_SESSION['recent']))
{
$_SESSION['recent'][$productimgfolder] = array(
'id' => $productimgfolder,
'title' => $product['Product']['title'],
'link' => $_SERVER['REQUEST_URI'],
'image' => 'img/products/'.$productimgfolder.'/'.$product['Product']['mainpicture']
);
}
$_SESSION['recent']=array_slice($arr, -4, 4, true);
other wise you have to foreach the recent array and check for id in the loop...

A different solution may be;
array_unshift($sessionArray, $singleArrayElement);
if (count($sessionArray) > 4) {
array_pop($seassionArray);
};

You need to check isset
if(!isset($_SESSION['recent']))
{
$_SESSION['recent'] = array()
}
THEN YOU CAN CHECK IF SESSION IS EMPTY
if(empty($_SESSION['recent']))
{
//here you add your data
}
because you are using array push, on page reload it adds data unless you check if session is empty. if yes you add data to your session.

Related

Insert dynamic Object property names into Database

Alright so I have an insert query that I would like to run but the issue I am having is with getting object properties/values that I need to insert.
Say I have a query that looks like the one below.
$this->db->insert('tblitems_in', array(
'platform' => $item['Platform'],
'ram' => $item['RAM'],
'qty' => $item['qty'],
'rate' => number_format($item['rate'], 2, '.', ''),
'rel_id' => $insert_id,
'rel_type' => 'estimate',
'item_order' => $item['order'],
'unit' => $item['unit']
));
This works fine when the person chooses RAM on the webpage which sets the $item Objects property 'RAM' to the value that was picked. Now if they choose HardDrive, that properties name is now sent as 'HardDrive' with the value they chose. Is there a way that i Could replace the 'ram' and 'RAM' from the below example with a variable so I could change what the property name is that I would like to insert and insert into the corresponding db column?
EDIT:
I should have added that the options on the webpage are also dynamically created from a database so I do not know at the time of coding what the property names are. They could be RAM, HardDrive, Processor, maybe even Elephant. I was hoping I could use variables so that I could look at the DB used to create the webpage so that I know the property names and then dynamically add those names into the query.
EDIT:
Right now I am using the following code in order to get all the possible options that can be received from the webpage from a DB the webpages uses to create itself.
$plat_options = $this->db->get('tblplatform_options')->row()->name;
In the database right now it is only populated with names RAM and HardDrive to make things known for testing purposes. So this returns $plat_options = {RAM, HardDrive}. I now have to figure out how to test is $item has these(RAM and HardDrive) as properties and if $item does have them then add them into the query previously shown.
You can set an array of key => variable names, then loop over those values to see if they exist in the $item variable and, if so, add that value to the data to be inserted into the db:
//default array of data to insert
$data = [
'platform' => $item['Platform'],
'qty' => $item['qty'],
'rate' => number_format($item['rate'], 2, '.', ''),
'rel_id' => $insert_id,
'rel_type' => 'estimate',
'item_order' => $item['order'],
'unit' => $item['unit']
];
//Get column names from db
$plat_options = $this->db->get('tblplatform_options')->row()->name;
// $plat_options = [RAM, HardDrive]
//Check if $item[$name] exists. If it does, add that to the
// array of data to be inserted
foreach($plat_options as $key) {
if(array_key_exists($key, $item)) {
$data[$key] = $item[$key];
}
}
$this->db->insert('tblitems_in', $data);
edit
I'm not sure this will work (I don't understand the use case).
It is possible, using array_diff_key to get a list of array keys that exist in $item but not in $data. With this array of keys, you can add the missing keys.
I have altered my previous code to demonstrate this.
You could create the array one element at a time based on whatever field data you received. I used a switch statement, but it could be a simple if/then/else as well.
$data_array = array();
$data_array['platform'] = $item['Platform']
switch($item['Object'] {
case 'HardDrive':
$data_array['harddrive'] = $item['HardDrive'];
break;
case 'RAM':
$data_array['ram'] = $item['RAM'];
break;
}
$data_array['qty'] = $item['qty'];
$data_array['rate' = number_format($item['rate'], 2, '.', '');
$data_array['rel_id'] = $insert_id;
$data_array['rel_type' = 'estimate';
$data_array['item_order'] = $item['order'];
$data_array['unit'] = $item['unit'];
$this->db->insert('tblitems_in', $data_array);

Calling many Variables stored in outside file

The code I had help with the last couple weeks works great. The problem is it's creating way more needed includes and external files than I first thought and getting to be a challenge to keep track of.
I was told to use MySQL. That would be fine if the data was going to be used over again. The data is only used long enough to build the pages, print to pdf and then it's no longer needed and the files are deleted.
I have three templates that are used to create all the needed pages. Only the data is different but never the same to allow it to be saved beyond it's use.
The problem I started having is when 30+ pages are loaded into a single browser window so it can get processed to pdf, this is calling a few hundred includes and some are being missed. When each page is called by itself it all loads fine.
The other thing I can think of is to try and get the variables belonging to each page in it's own single file and have the page access that file. When I call the file with include "file.php"; it just prints everything to the screen and not where they are needed. That way each file would have 10 - 15 variables in it for each page This would eliminate over 400 external files down to 1+ images for each page.
Is putting them all in one separate file and then called possible?
I hope I explained this correctly.
Thanks in advance.
// What I would like in one file.
$item1 = "Data for Item one";
$photo1 = "img src string to image for item 1";
etc...
$item12 = "Data for Item 12";
$photo12 = "img src string to image for item 12";
This would then call the items in the proper location of the page.
echo "$item1";
echo "photo1";
etc...
echo "$item12";
echo "photo12";
You can use a MySQL database to store information like that. (Settings, etc.)
But you can also use arrays for this which is probably more advisable.
Solution with arrays
An array gives you the possibility to easily store and manage data of a similar type.
You create a new array like this:
$settings = array();
To store a value in it, you have several options:
name it as an integer (0, 1, 2, 3 etc.)
name it as a string ('item1', 'path2' etc.)
$settings = array('path1', 'path2');
This just stored 0 => 'path1' and 1 => 'path1'
To get the value of a key in an array:
echo $settings[0]; //or $settings{0}, outputs 'path1'
echo $settings[1]; //outputs 'path2'
Or you store it as a string:
$settings = array('picture1' => 'path1', 'picture2' => 'path2');
echo $settings['picture1']; //outputs 'path1'
Also, multidimensional arrays are possible:
$settings = array(
'paths' => array(
'picture1' => 'path1',
'picture2' => 'path2'
),
'language' => 'english'
);
You get a value of a multidimensional array like this:
//for every dimension a new [], outputs 'path1'
echo $settings['paths']['picture1'];
Then you can just easily store all your settings and require_once 'settings.php';.
If you want to learn more about arrays, go to the php.net documentation.
Example:
/php/settings.php
$settings = array(
'items' => array(
'item1' => 'Data for Item1',
'item12' => 'Data for Item12',
),
'photos' => array(
'photo1' => 'img src string to image for item 1',
'photo12' => 'img src string to image for item 12',
),
);
index.php
<?php
require_once '/php/settings.php';
echo $settings['items']['item1']; //outputs 'Data for Item1'
//Or you can even use a foreach loop
foreach($settings['items'] as $key) {
echo $key;
echo '<br>';
}
That prints out:
Data for Item1
Data for Item12
Hope this helped.

Return Different PHP Array Values Based on A User ID-Related Constant

I am working with an array of tokens for an HTML template. Two of them ('{SYS_MENU}' and '{SUB_MENU}') are used to generate control buttons for the web application. Right now the buttons show up on the login page before the user's credential's are validated, and I need to change the code so that the buttons are hidden until after users login and reach the main menu. When someone types the http: address into their browser and arrives at the login page the system starts a session for them in the MySQL sessions table with USER_ID = 0. After they login the USER_ID changes to whatever number was assigned to them at initial registration (Example: USER_ID = 54), and after they logout at the end of the session back to 0. Tying this constant to the buttons seems like the best solution and I have found it to work in the past under similar circumstances.
Here is the original array:
$template_vars = array(
'{LANG_DIR}' => $lang_text_dir,
'{TITLE}' => theme_page_title($section),
'{CHARSET}' => $charset,
'{META}' => $meta,
'{GAL_NAME}' => $CONFIG['gallery_name'],
'{GAL_DESCRIPTION}' => $CONFIG['gallery_description'],
'{SYS_MENU}' => theme_main_menu('sys_menu'),
'{SUB_MENU}' => theme_main_menu('sub_menu'),
'{ADMIN_MENU}' => theme_admin_mode_menu(),
'{CUSTOM_HEADER}' => $custom_header,
'{JAVASCRIPT}' => theme_javascript_head(),
'{MESSAGE_BLOCK}' => theme_display_message_block(),
);
The first thing I did was to work with the references directly in the HTML template. I saw an example on w3schools that made it look like you could just type a PHP script into HTML and have it resolve. That didn't do anything except echo a bunch of text randomly into the page. I then found another citation that said you had to activate the PHP with an .HTACCESS entry before it would work directly in HTML. But that didn't close the deal either.
I know that changing '{SYS_MENU}' and '{SUB_MENU}' values in the array to => "", produces the results that I want (I.E. make the menu buttons disappear). So my next thought was I'll create an IF statement that returns two versions of the array based on circumstances, something like:
if(USER_ID != 0)
{
return $template_vars = //FIRST VERSION OF ARRAY WITH FULL VALUES//
}
else
{
return $template_vars = //SECOND VERSION OF ARRAY WITH ONLY => ""//
}
But all that did was cause the application load to terminate at a white screen with no error feedback.
My most recent attempt came from something I read here on Stack Overflow. I know that you cannot put IF statements into an array. But the article at this link described a workaround:
If statement within an array declaration ...is that possible?
So I rewrote the array as follows:
template_vars = array(
'{LANG_DIR}' => $lang_text_dir,
'{TITLE}' => theme_page_title($section),
'{CHARSET}' => $charset,
'{META}' => $meta,
'{GAL_NAME}' => $CONFIG['gallery_name'],
'{GAL_DESCRIPTION}' => $CONFIG['gallery_description'],
'{SYS_MENU}' => ('USER_ID != 0' ? theme_main_menu('sys_menu') : ""),
'{SUB_MENU}' => ('USER_ID != 0' ? theme_main_menu('sub_menu') : ""),
'{ADMIN_MENU}' => theme_admin_mode_menu(),
'{CUSTOM_HEADER}' => $custom_header,
'{JAVASCRIPT}' => theme_javascript_head(),
'{MESSAGE_BLOCK}' => theme_display_message_block(),
);
But that seems to have no effect at all. The application doesn't crash but the buttons are static whether you are logged in or logged out.
My question is: What am I missing? I can see that this is possible. But I've been trying things for a day and a half and just seem to be dancing around the solution. Your thoughts would be greatly appreciated.
The problem here is that you are calling return. With a global include file like this there is not context to return to so the application terminates. What you want to do is just assign the variables.
if(USER_ID != 0)
{
$template_vars = //FIRST VERSION OF ARRAY WITH FULL VALUES//
}
else
{
$template_vars = //SECOND VERSION OF ARRAY WITH ONLY => ""//
}

Set field value for single database row

I'm trying to update a row on my profiles table to reset a users profile picture to the default of user.png. I have the following action in my controller:
public function deleteProfilePicture() {
$this->layout = 'ajax';
// First find the profile ID from the user ID
$profileId = $this->Profile->find('first', array(
'condition' => array('User.id' => $this->Auth->user('id')),
'fields' => array('Profile.id'),
'recursive' => -1
));
$this->Profile->id = $profileId['Profile']['id'];
$this->Profile->saveField('picture', 'user.png', false);
}
However, when I request the URL (/profile/deleteProfilePicture) I get no errors but the database row isn't updated. I have made sure the current profile ID is used by using debug($profileId).
What could be going wrong here?
Edit: The return value of saveField():
array(
'Profile' => array(
'id' => '36',
'modified' => '2013-04-05 14:16:57'
)
)
Try
$this->Profile->id = $profileId['Profile']['id'];
$this->Profile->set(array(
'picture' => 'user.png'
));
$this->Post->save();
I see no error in your code. Try seeing what query is getting executed using this
$log = $this->Model->getDataSource()->getLog(false, false);
debug($log);
based on the result make changes to your query if you find something wrong.
Or try using this
$data['Profile']['id']=$profileId['Profile']['id'];
$data['Profile'['picture']='user.png';
$this->Profile->save($data);
If you set debug to 2 you could see what SQL is being executed, see if your update is actually firing.
Try this
$this->Profile->read(null, $profileId['Profile']['id']);
$this->Profile->saveField('picture', 'user.png', false);
Why are you setting the validation to false? Do you get an error if you omit that?

Codeigniter says session userdata blank, but exists in db

Whenever i try check for a value in my userdata column of my session it thinks it's blank, when it's actually in the ci_sessions table serialised.
here's the db content unserialised:
array (
'user_data' => '',
'edit' =>
array (
'image_id' => 'HF',
'session_id' => '783c15b057bcd9c19d3fd82f367ee55d',
),
)
here's how im checking for the userdata in my view (note sessions are autoloaded)
<?php if ($this->session->userdata('edit')) : ?>
enter code here
and here's how i set the session userdata:
$values = array(
'image_id' => implode(",",$uploaded_image_ids),
'session_id' => $this->session->userdata('session_id')
);
$this->session->set_userdata('edit', $values);
Can anyone see the problem? (whole controller here http://pastebin.com/aXeRn1VN)
Also heres my config.php http://pastebin.com/A31nrC1b
View variables are separate from other variables in CI. I think (am still a bit of a CI newbie) that $this->session is not available in Views. This CI forum post discusses a couple of possible solutions to the problem http://codeigniter.com/forums/viewthread/100587/#508098 - one is to pass the value of 'userdata' from the session into the $data array you pass to the view - which seems the best solution.
You can use like this
<?php $edit = $this->session->userdata('edit'): if (!empty($edit)) : ?>

Categories