I'm working on a PHP project the teacher gave us, basically we have to make a website dynamic and create a liste.php with tasks stored in an API the teacher has.
So I have the index.php with the front page on it and the list of all the projects going on ( like trip preparation, shopping list and appartment decoration ). I created a liste.php with all the tasks contained in each project ( buying tickets, booking the hotel and buying a postcard for the trip preparation ).
Now I have to get all the tasks of each project from the API he gave us.
Here's the API
http://todo_api.xx.firstname-lastname.com/tache.php?liste_id=1
That's the API for project n°1, there are 3 projects in total ( I mentionned them all above )
I also have a functions.php with the $api_url stored, and 2 functions, one getting the list of projects, the other getting the tasks for each project.
$api_url = "http://todo_api.xx.firstname-lastname.com/";
function getAllListes(): array {
global $api_url;
$json = file_get_contents($api_url . "liste.php");
return json_decode($json, true);
}
function getAllTaches(): array {
global $api_url;
$json = file_get_contents($api_url . "tache.php?liste_id=" . $id);
return json_decode($json, true);
}
But when I try to print_r that, or even just display each task, I get this :
Notice: Undefined index: liste_id in /Applications/MAMP/htdocs/todo/database/functions.php on line 17
So obviously something is wrong, and this keeps me from going on. Any idea ?
Edit : here's my html/php code regarding the tasklist
$listes = getAllListes();
$taches = getAllTaches($id);
<ul>
<?php foreach ($taches as $tache) : ?>
<li><?php echo $tache["libelle"] ;?></li>
<?php endforeach ; ?>
</ul>
libelle is the index of the information required.
Thanks a lot !
Your code in functions.php
function getAllTaches(): array { //declared function without parameter
but html/php code
$taches = getAllTaches($id);// calling the function with parameter.
i guess if you pass the parameter to the function it will work.
Related
I have a project in which is develop in codeigniter.
The project is like this:
-root
--controller
---Slider.php
--models
---My_data.php
--views
---slider_view.php
---giris.php
Slider.php
public function manset_al(){
$title['title']='manşet listesi';
$this->load->model('My_data');
$data['manset']= $this->My_data->get_manset();
$this->load->view('slider_view' ,$data);
}
My_data.php code is;
public function get_manset() {
// $sorgu =
// mysql_query("select * from manset, haberler
// where manset.onay='1' and manset.link=haberler.id and haberler.onay='1'
// order by haberler.id desc");
$this->db->select('*');
$this->db->from('manset as man, haberler as hab');
$this->db->where('man.link=hab.id');
$this->db->where('hab.onay=1');
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->result();
} else {
return $query->result();
}
slider_view.php code is:
foreach($manset as $row){
$baslik =$row->baslik;
$icerik =$row->icerik;
$link =$row->link;
$img_path =$row->img_path;
$giris =$row->giris;
$cikis =$row->cikis;
echo '
<div>
<img data-u="image" src="'.$img_path.'"/>
<div data-u="thumb"> <h5 style="color:white; margin:10px;">'.$baslik.' </h5> </div>
</div>';
}
Now when i called
http//example.com/index.php/Slider/manset_al
every think is ok - the slider is running.
But when i get the slider in giris.php with this code;
$this->load->view('slider_view');
it's not run and say: undefined variable manset;
how can ı fix it?
Thank you.
We're missing some details to quickly solve this one: namely, how is giris.php called, exactly? Without those details I can only give a general answer.
This error means that you are trying to access data in your template file that you haven't passed to the templating system. For instance, in your controller (Slider.php) you pass data off to the templates here:
$this->load->view('slider_view' ,$data);
The $data is the important part: codeigniter takes every entry in that array and creates a variable in the template system whos name corresponds to the name of each key. Therefore, foreach( $manset as $row ) works because $data has a key named manset that has each row in it. You can see that in giris.php you are specifically not sending any data to the template:
$this->load->view('slider_view');
Notice the lack of a second parameter. Of course your file structure implies that giris.php is itself a view file, which means that whatever controller method that called giris.php needs to be the one sending data out to the view. So generally, that is the answer: whatever controller is ultimately calling giris.php needs to be sending the slider information out to the templating system, but isn't. Without more details on how giris.php is actually called, that is as specific as I can get.
I have bought one android application with web service made in codeigniter. There API in this web service is like below.
<?php
if (!defined("BASEPATH"))
exit("No direct script access allowed");
class Site extends back {
public function __construct() {
parent::__construct();
$this->lang->load("site", "english");
$this->load->model("site_model", "site");
}
//=====================================================================
public function get_updates($last_author, $last_quote) {
$this->db->where("_auid > ", $last_author);
$this->db->where("au_status", 1);
$result["authors"] = $this->db->get("authors")->result_array();
$this->db->select("quotes.*, authors._auid, authors.au_status");
$this->db->from("quotes");
$this->db->join("authors", "authors._auid = quotes.qu_author AND authors.au_status = 1");
$this->db->where("_quid > ", $last_quote);
$this->db->where("qu_status", 1);
$result["quotes"] = $this->db->get()->result_array();
echo json_encode($result);
}
}
I am learning php yet. I have made another fresh corephp web service for use. I am not understanding above api and so not able to make similar api for my new web service. Both web service use same database...anyone can please suggest me how can I use above API in my new corephp web service ?
sorry for my bad knowledge.
Thanks
It is very simple
function get_updates get 2 parameter as input
1) $last_author //value of _auid(id) field belong to table author
2) $last_quote //value of _quid(id) field belong to table quotes
$this->db->where("_auid > ", $last_author);
$this->db->where("au_status", 1);
$result["authors"] = $this->db->get("authors")->result_array();
These lines fetch data from table author with matches value of $last_author parameter
And second query fetch data from table quotes and it is join with author with matches value of $last_quote parameter.
Join is use for smashing two or more tables into a single table.
$last_author and $last_quote is request parameter send from application.
Desired result will be stored in $result and return with json object as response data to application.
I am trying to add labels on product list page based on special conditions. I have buy one get one free sale, so When user visits buy 1 get 1 free category, he should be able to see the label on the products(in my case, I have bogo.png image). Everything worked fine with the modifications I did until I searched on the store front for a product, I get undefined variable error.
2015-01-09 18:26:58 - PHP Notice: Undefined index: bogo in catalog/view/theme/YourTheme/template/product/product_collection.tpl on line 26
I did google search for the problem and browsed opencart forums for days without any luck. So here is what I did until now. On Category.php in catalog/controller file, I added under this array$this->data['products'][]= array(
'bogo' => $bogo,
And added this condition under getProducts specifying if the category id is the category id for the buy 1 get 1 free category set bogo to true.$results = $this->model_catalog_product->getProducts($data);
if($category_id==977){
$bogo = true;
}
else{
$bogo = false;
}
and on product_collection.tpl file, I did this change.
<pre><code>
<?php if( $product['bogo'] ) { ?>
<span class="product-label-bogo2"><img src='bogo.png'></span>
<?php } else if ($product['special']) { ?>
<span class="product-label-special"><span><?php echo $this->language->get( 'text_sale' ); ?></span></span>
<?php } ?>
</code></pre>
Everything is fine if I go to that category it displays the label perfectly, the problem is I get that above error only when I search for anything on store front.
Please note that before rating the question negatively, I am not familiar at all with php and I did my best with research for hours to solve this.
you need to add that piece of code
if($category_id==977){$bogo = true;}else{$bogo = false;}
$this->data['products'][]= array('bogo' => $bogo,
to controller/product/search.php => ControllerProductSearch#index
BTW: you will need the add the above code in every controller file that makes use of product/product_collection.tpl
I have a custom joomla MVC component created by http://component-creator.com with 4 tables:
#__mycomponent_items 27 Fields
#__mycomponent_bids 12 Fields
#__mycomponent_journeys 9 Fields
#__mycomponent_users 8 Fields
I am trying to set the relationships between these tables, but in the absence of documentation and experience I am struggling.
The basic relationships between the tables need to allow USERS to make BIDS to deliver ITEMS.
So I have created fields for items like this:
#__mycomponent_items
id
created
updated
ordering
state
checked_out
checked_out_time
created_by
deliverydestination
itemtitle
status
requiredby
listprice
deliveredprice
commission
points_reward
accepted_bid
accepted_bidder
accepted_journey
And for bid like this:
#__mycomponent_bids
id
state
created_by
item_id
buyer
bid
created
updated
bid_status
bid_expires
journey
arrival_date
I am working in templates/mytemplate/html/com_mycomponent/item/default.php and trying to add to that view a list of the current bids on that item. To do that I assume I need to add a custom function to /components/com_mycomponent/models/item.php and the function I have created is follows:
function itemBids() {
// Get a db connection.
$db = JFactory::getDbo();
// Create a new query object.
$query = $db->getQuery(true);
// Select item record matching the $orderID
$query
//->select('*')
->select($db->quoteName(array('id', 'created_by', 'created', 'bid', 'bid_status', 'arrival_date')))
->from($db->quoteName('#__mycomponent_bids'))
->where('item_id = item.id');
// Reset the query using our newly populated query object.
// Load the results as a list of stdClass objects (see later for more options on retrieving data).
$db->setQuery($query);
$itemBids = $db->loadObjectList();
//print_r($itemBids);
}
How do I then access the data in the view /mytemplate/html/com_mycomponent/item/default.php?
I have tried this and it returns nothing:
<ul>
<?php foreach ($itemBids as $itemBid) :?>
<?php $arrivalDate = $itemBid->arrival_date; ?>
<li><strong><?php echo $itemBid->created_by; ?></strong> <small>can deliver for</small> $<?php echo $itemBid->bid;?> <small>
<?php /*?><abbr class="timeago" title="<?php echo $itemBid->created; ?>"></abbr><?php */?>
in <strong><abbr class="timeago" title="<?php echo $arrivalDate; ?>"></abbr></strong></small><div class="uk-badge uk-float-right"><?php echo $itemBid->bid_status; ?></div></li>
<?php endforeach; ?>
</ul>
You wouldn't be putting it in your template like that. the template just holds the layouts that generate the html. Also you need to have a default layout in your views/myview/tmpl folder.
You don't seem to be returning anything from your itemBids() function. You would want to add return $itemBids; or possibly return $this->itemBids; depending on what you are doing elsewhere.
YOu want to get $this->itemBids in your view.html.php class so that it is then available to your layout. THen instead of referring to $itemBids you can refer to $this->itemBids in your loop in the layout.
Have you been through the Creating an MVC Cmponent tutorial? It would probably help you get a sense of how MVC works in Joomla.
OK , as far as I understand you have a method in yourmodel.php and you are trying to access it from the view I did notice that you are not returning any values in your method
return $db->loadObjectList();
let me just make it simple by the below code
//com_component/models/yourmodel.php
class componentModelYourmodel extends JModelLegacy
{
function yourMethod()
{
//code
return $value;
}
}
And then in view file
//com_component/views/yourview/tmpl/default.php
//get the model first
$model=$this->getModel();
//call the method
$items=$model->yourMethod();
//print
print_r($items);
I'm trying to display the subscriber count from a MailChimp mailing list using their API, and I've got it partially working, except the code below is currently spitting out the subscriber count for all lists, rather than for one specific list. I've specified the list id in the line $listId ='XXX'; but that doesn't seem to be working. Because I have five lists in total, the output from the PHP below shows this:
10 0 0 1 9
What do I need to do in my code below to get the subscriber count from a specific list id?
<?php
/**
This Example shows how to pull the Members of a List using the MCAPI.php
class and do some basic error checking.
**/
require_once 'inc/MCAPI.class.php';
$apikey = 'XXX';
$listId = 'XXX';
$api = new MCAPI($apikey);
$retval = $api->lists();
if ($api->errorCode){
echo "Unable to load lists()!";
echo "\n\tCode=".$api->errorCode;
echo "\n\tMsg=".$api->errorMessage."\n";
} else {
foreach ($retval['data'] as $list){
echo "\t ".$list['stats']['member_count'];
}
}
?>
I just came across this function (see below) that let's me return a single list using a known list_id. The problem is, I'm not sure how to add the list_id in the function.
I'm assuming I need to define it in this line? $params["filters"] = $filters;
The MailChimp lists() method documentation can be referred to here: http://apidocs.mailchimp.com/rtfm/lists.func.php
function lists($filters=array (
), $start=0, $limit=25) {
$params = array();
$params["filters"] = $filters;
$params["start"] = $start;
$params["limit"] = $limit;
return $this->callServer("lists", $params);
}
I'd strongly recommend not mucking with the internals of the wrapper as it's not going to be nearly as helpful as the online documentation and the examples included with the wrapper. Using the wrapper means the line you tracked down will effectively be filled when make the proper call.
Anywho, this is what you want:
$filters = array('list_id'=>'XXXX');
$lists = $api->lists($filters);
Mailchimp provides a pre-built php wrapper around their api at http://apidocs.mailchimp.com/downloads/#php. This api includes a function lists() which, according to its documentation, returns among other things:
int member_count The number of active members in the given list.
It looks like this is the function which you are referring to above. All you should have to do is iterate through the lists that are returned to find the one with the proper id. From there you should be able to query the subscriber count along with a number of other statistics about the list.