I am trying to build a simple shopping cart using PHP and no Database.
Based on the user input, I search for an Item using ebay API(returns XML Data).
I am able to get its price,id,and other details.
I am then creating an array called ITEMS which contains all the data returned by ebay.
(I know I have to create a cart only for items that are selected).
The Issue is I am able to access the cart contents from the session within the search function but not from other functions.I am new to PHP could someone help me fix this.
As of now I am only trying to access the cart contents from buy.
function search(){
$xml = new SimpleXMLElement($xmlstr);
print "<table border=1>";
$loop = $xml->categories[0]->category->items->product;
foreach ( $loop as $dummy) {
$id = $dummy->attributes();
$link = $dummy->productOffersURL;
$name = $dummy->name;
$price = $dummy->minPrice;
$image = $dummy->images->image->sourceURL;
}
array_push($ITEMS, $item);
}
}
I changed the for eeach loop and it worked:
The code is :
$id = (String) $dummy->attributes();
$link = (String)$dummy->productOffersURL;
$name = (String)$dummy->name;
$price = (String)$dummy->minPrice;
$id = (String) $dummy->attributes();
$link = (String)$dummy->productOffersURL;
$name = (String)$dummy->name;
$price = (String)$dummy->minPrice;
You shouldn't call session start with the # (error supression) modifier. It could be triggering and error and you wouldn't know.
Check if your php.ini configuration is set to autostart the session.
session.auto_start = 0
If it's set to 1, you could be overwriting its contents by manually calling session start every time.
Related
Solved the problem earlier today. In the 2nd line of code global $db,$tags; was overwriting if($this->has_lead_type_selected($person['ID'],$tags)) which caused the global $tags to overwrite $tags at the lower portion of code. So the global var was empty because it was before the actual $tags var was given a function.
When an HTML form is submitted to our REST API it sends data called 'lead_type' which are simply tags to identify the lead being sent.
User select these tags from a tag cloud. If a form is submitted to the API with one of these tags (lead_type) and any of our users profiles match those tags (they selected in their tag cloud). The user is sent a SMS to notify them.
Everything posts to the database tables, the API works but everyone of the users still gets a SMS even if they don't have matching tags. If I comment out the line (i'll show the rest of the code below) an SMS is sent to everyone. If I leave it uncommented no SMS is sent to anyone.
if($this->has_lead_type_selected($person['ID'],$tags))
Here is how the code flows.
}
private function has_lead_type_selected($user_id,$tags){
global $db,$tags;
$lead_types = explode(',',$tags);
$user_lead_types = $db
->where('user_id',$user_id)
->where('lead_type_id', $lead_types, 'IN')
->get('user_lead_types');
return sizeof($user_lead_types) > 0;
}
//Get lead types from API post and create $tags
$lead_types = $this->request['leadData']['lead_types'];
$strTags = array();
if(!empty($lead_types))
$strTags = explode(',',$lead_types);
$tags = '';
$lead_types_objects = $db->where('lead_type', $strTags,'IN')->get('lead_types');
foreach($lead_types_objects as $l)
{
if($tags=='')
$tags = $l['id'];
else
$tags.=',' .$l['id'];
}
We then send them a SMS if the form tags matched the users cloud tags.
global $sid,$token;
$client = new Twilio\Rest\Client($sid, $token);
$content_data = [
"leadname" => $posted_name,
"leadzipcode" => $posted_zipcode,
"leadphone" => $posted_phone,
"leademail" => $posted_email,
"leadtags" => $lead_types
];
//Replace Content
foreach($content_data as $index => $value){
$lead_sms_template = str_replace("|".$index."|", $value, $lead_sms_template);
}
// Step 5: Loop over all our friends. $number is a phone number above, and
// $name is the name next to it
foreach ($people as $person) {
try{
//commented temporarily -- uncommented below to try to solve issue of texting everyone still
if($this->has_lead_type_selected($person['ID'],$tags))
{
$number = $person['phone_no'];
$name = $person['first_name']. ' '. $person['last_name'];
Thank you for the help.
In the 2nd line of code global $db,$tags; was overwriting if($this->has_lead_type_selected($person['ID'],$tags)) which caused the global $tags to overwrite $tags at the lower portion of code. So the global var was empty because it was before the actual $tags var was given a function.
I'm having some problems trying to access variables sessions in the confirmation page used it by payu. In the post request I can access the variables sent from the gateway like this: $state_pol = $_POST["state_pol"]; and no problem with that, I can stored the value in my data base, But when I try to do this $email = Auth::user()->email; when I do that I can't get any variable or stored values in the DB. I need to stored in the DB the cart Items and I'm using moltin Cart, so I need to do something like this:
foreach (Cart::contents() as $item) {
$citem = new Ite;
$citem->compra_id = $compra->id;
$citem->id_product = $item->id;
$citem->name = $item->name;
$citem->val_uni = $item->price;
$citem->image = $item->image;
$citem->iva = $item->tax;
$citem->qty = $item->quantity;
$citem->val_total = $item->total();
$citem->save();
}
Cart::destroy();
the methods Cart::contents() and Cart::destroy(); don't work, but just in the payu request, I tried this with paypal request and everything works correctly and I'm using the same code, I mean with the variables session.
Thanks in advance for the time and help.
I just realized that the problem is with eloquent too. I mean, I cannot get access to the DB, I cannot do something like this $user = User::where('id','=',1)->first(); I tried with query builder but it didn't work.
I'm trying to display stored data form the mongodb using the php? But it display's all the header file including data. How to ignore the header information?
It shows like
{ "_id" : ObjectId("550ee694c5c9f2729b066c23"),
I want result as
550ee694c5c9f2729b066c23
my php code:
$db = new Mongo();
$query = $db->selectDB('test');
$collections = new MongoCollection($query,'demo');
$coursor = $collections->find();
foreach ($coursor as $doc)
{print_r($doc);}
You can find the value of the ObjectId() object as a lowercase hexadecimal string using valueOf() method.
ObjectId("550ee694c5c9f2729b066c23").valueOf() = 550ee694c5c9f2729b066c23
For more details you can check documentation :
http://docs.mongodb.org/manual/reference/method/ObjectId.valueOf/
Actually { print_r($doc); } shows header too. There should be some kind of a Key to know which data we need. So we cannot remove them.
$db = new Mongo();
$query = $db->selectDB('test');
$collections = new MongoCollection($query,'demo');
$coursor = $collections->find();
foreach ($coursor as $doc)
{
echo $doc['id'];
}
Will give you only 550ee694c5c9f2729b066c23
I have a php file setup to pull through ONE XML data feed, What I would like to do is load up to 4 feeds into it and if possible make it select a random item too. Then parse that into an jQuery News Ticker.
My current PHP is as follows...
<?php
$feed = new DOMDocument();
$feed->load('/feed');
$json = array();
$json['title'] = $feed->getElementsByTagName('channel')->item(0)->getElementsByTagName('title')->item(0)->firstChild->nodeValue;
$json['description'] = $feed->getElementsByTagName('channel')->item(0)->getElementsByTagName('description')->item(0)->firstChild->nodeValue;
$json['link'] = $feed->getElementsByTagName('channel')->item(0)->getElementsByTagName('link')->item(0)->firstChild->nodeValue;
$items = $feed->getElementsByTagName('channel')->item(0)->getElementsByTagName('item');
$json['item'] = array();
$i = 0;
foreach($items as $item) {
$title = $item->getElementsByTagName('title')->item(0)->firstChild->nodeValue;
$description = $item->getElementsByTagName('description')->item(0)->firstChild->nodeValue;
$pubDate = $item->getElementsByTagName('pubDate')->item(0)->firstChild->nodeValue;
$guid = $item->getElementsByTagName('guid')->item(0)->firstChild->nodeValue;
$json['item'][$i++]['title'] = $title;
$json['item'][$i++]['description'] = $description;
$json['item'][$i++]['pubdate'] = $pubDate;
$json['item'][$i++]['guid'] = $guid;
echo '<li class="news-item">'.$title.'</li>';
}
//echo json_encode($json);
?>
How can I modify this to load more than one feed into the file?
Thanks in advance
The simplest approach to doing this is wrapping another loop around the code you have. It's not the cleanest way but will probably suffice for the purpose.
In general, IMO, it's always beneficial to learn the basics of the language first. E.g. PHP manual on foreach
This is roughly what the loop needs to look like:
$my_feeds = array("http://.....", "http://.....", "http://.....");
foreach ($my_feeds as $my_feed)
{
// This is where your code starts
$feed = new DOMDocument();
$feed->load($my_feed); <--------------- notice the variable
$json = array();
... and the rest of the code
}
this will walk through all the URLs in $my_feeds, open the RSS source, fetch all the items from it, and output them.
If I'm reading your question right, what you may want to do is turn your code into a function, which you would then run inside a foreach loop for each url (which you could store in an array or other data structure).
Edit: If you don't know much about functions, this tutorial section might help you. http://devzone.zend.com/9/php-101-part-6-functionally-yours/
In my code i am trying to store a variable between two pages but i either get a string return "images" or 0... or nothing- tried casting it.. echoing in on one page works and displays correct number but as soon as you click through the view to the next page- its lost- i tried turning on cs_sessions in the db and made no difference
<?php
public function carconfirm($car_id = '')
{
if(empty($car_id))
{
redirect('welcome');
}
$this->load->model('mcars');
$car = $this->mcars->getCar($car_id);
$data['car'] = $car->row_array();
$car_id = (int) $car_id;
$this->session->set_userdata('flappy', $car_id);
echo $car_id;
//insert details
//display details
$this->load->view('phps/carconfirm',$data);
}
function confirm()
{
//get vars
$time_slot = $this->session->userdata('slot');
$person_id = $this->session->userdata('person_id');
$car_id = $this->session->userdata('flappy');
$insert_array = array( 'slot'=> $time_slot ,
'car'=> $car_id,
'person_id'=> $person_id
);
print_r($insert_array);
$this->load->model('mbooking');
$result = $this->mbooking->addbooking($insert_array);
if($result)
{
redirect('welcome/options');
}
}
?>
the variable I'm losing is flappy- i changed the name to see if that was the problem
Finally, I fixed this. Using this answer in SO too : codeigniter setting session variable with a variable not working, I scan my js/css for missing resources. Turn out that, my thickbox.js refer to loadingAnimation.gif in, yes, images folder. That's where the images come. Having fix the missing file, the sesion variabel working just fine.
Not sure, why CI replace (maybe) last added session variabel using this, but maybe it's because CI is not using $_SESSION global variabel. CMIIW. I use this article as a hint : http://outraider.com/frameworks/why-your-session-variables-fail-in-codeigniter-how-to-fix-them/