How to construct multiple id values to a curl request - php

To explain more. I have a current data-store which will feed a .json file from a remote server. I can grab this remote file through an id added to a curl function.
ie. $json_url = 'example.com/datastore.json?id='. $result['id'].'';
This example is housed within a foreach loop pulling the id's from a database. This json_url example works for requesting 1 id at a time. I have instituted on the remote server the ability to request multiple ids at once through comma separated values.
ie. $json_url = 'example.com/datastore.json?id=1,id=2,id=3
Now if I am building the json_url on the fly for multiple requests, how should I do this?
I want this type of a $json_url result:
`$json_url = 'example.com/datastore.json?id=1,id=2,id=3`
BUT those id values are id values that have been returned from the foreach loop, and with each id value comes an added json file and an added id value to the json_url. In this case instead of 1, it is 3 .json files I will have retrieved with 1 request to the server, the number here is ambiguous as it is ultimately dependent on how many id values are found on the query.
If I am unclear with my examples, let me know and I will take the time to make it more clear.
`

Why not using an array?:
$json_url = 'example.com/datastore.json?id[]=1&id[]=2&id[]=3
then you can loop using
foreach($_GET['id'] as $key => $val){}
//$_GET['id'] OR $_POST['id']...

Related

Adding a new field to data extracted from a database call

As part of a search routine for a specific crop, I'm making two calls to the same database table, merging the results and sending them down the line. The first call looks for data relating to the searched-for crop (e.g. "beans"). The second call looks for data relating to the crop group of that crop (e.g. legumes).
Data returned from the first call will be more relevant/focused than that from the second call. I want to add an identifier to the respective data sets that reflects this so that I can subsequently sort/present the data on the basis of relevance in my Vue component.
The following code extracts the crop-specific information from the database; how can I add/insert/append a new variable (e.g. "relevance" = 1) to each row in $factsheets before I "array_merge" it with the data returned from the crop-group sql call?
(For sake of simplicitly, I've not included the code that determines the crops.id value from the name of the crop entered by the user.)
public function getFactsheets($cropId){
$factsheets = Factsheet::whereIn('crop_id',$cropId)
->join("crop_factsheet as cf","factsheets.id","=","cf.factsheet_id")
->join("crops as crops","crops.id","=","cf.crop_id")
->select('crops.name','title', 'factsheets.id', 'shortdesc', 'shortimg', 'factsheets.slug')
->orderBy('crops.name')
->get()->toArray();
return $factsheets;
}
Thanks, Tom.
If you give & to value so whatever changes will happen to value will directly saved on its address.
foreach($factsheets as $key => &$factsheet){
$factsheet['relevance'] = 1;
}
Working demo.
Here is concise explanation of references in official doc.
you can simply do a foreach
foreach($factsheets as $key => $factsheet){
$factsheet['relevance'] = 1;
}

Storing multiple artist info from API into file (PHP)

I really need help. I'm using an API to gather artist information depending on the artist name ($artist_name = $_GET['artistname']).
I need to add the API results into an array and store it into a file (NOT a database). This file will be ever growing as more and more artist entries are added to it.
Once I have an array in the file, I need to be able to read it and parse it. That way I can display the information without repeatedly using the API.
I have figured out how to add an array to a file with one entry, but how can I add more keys into the same array?
This is what I'm using now...
//ARRAY
$artist_info_location_array = array($artist_name => $location_entry);
//FILE
$artist_location_file = get_template_directory()."/Database/Artists/info-location.json";
//GET ARRAY FILE
$get_location_array[] = json_decode(file_get_contents($artist_location_file), true);
if (is_array($get_location_array)) {
if (!array_key_exists($artist_name, $get_location_array)) {
file_put_contents($artist_location_file, json_encode($artist_info_location_array));
}
}
It prints this to the file:
{"Imagine Dragons":"Las Vegas, NV, US"}
That's cool, but I need to be able to add more artists to this SAME ARRAY. So the result should look like this with another artist added:
{"Imagine Dragons":"Las Vegas, NV, US", "Adele":"London, UK"}
That shows Imagine Dragons and Adele both added to the same array.
Can someone help me "append" or add extra keys and values to the same array as they are added to the file?
Thanks.
EDIT 1 (In response to Martin):
I have a panel on the side of the page in question. This panel will show relevant information about the artist that has been searched for. Let's say you search for the artist "Adele". $artist_name would = Adele.
Lets say I'd like to store all artist locations, I would use the example I posted to store each artist location in the file called info-location.json ($artist_location_file).
So every time an artist page is loaded, the artist name and location would be added to the array in the file.
If my example doesn't make any sense, please show me an example on how to add multiple entries into ONE ARRAY. I am using an API and would like to cache this information to use instead of requesting the API on each load.
Hope this makes sense. :)
I might be misunderstanding your question, but if you just want to read in a json file, add an associative array key to it if it does not exist and then put it back into the json file why dont you do something like this:
if (is_array($get_location_array)) {
if (!array_key_exists($artist_name, $get_location_array)) {
$get_location_array[$artist_name] = $location;
file_put_contents($artist_location_file, json_encode($artist_info_location_array));
}
}
file_put_contents will overwrite an existing file (pretty sure). But your best option is to use a database. If you can't do that, then I suggest to prevent writing to the file while you are doing this I suggest you use fopen, flock, and fwrite and then fclose

How can I define a variable in the PHP header ('Location:)

I'm trying to redirect to another page when a record is added to the database.
I want to make sure that this instruction creates an address which contains the id number.
header('Location:inventory_issue_view.php?filterer_client_name=<%%URLVALUE(suiting_reference_no)%%>');
However the address I get from this is
inventory_issue_view.php?filterer_client_name=<%%URLVALUE(suiting_reference_no)%%>
I know the value of
<%%URLVALUE(suiting_reference_no)%%>
is correct since if I edit the instruction to this:
header('Location:inventory_issue_view.php?filterer_client_name=7499');
I get the correct address.
Using this code:
$client=sqlValue("SELECT suiting_reference_no FROM client_suiting WHERE client_name = '{$data['client_name']}'");
I get a null value for $client
Using this code:
$client=sprintf("SELECT suiting_reference_no FROM client_suiting WHERE client_name = '{$data['client_name']}'");
$client=SELECT%20suiting_reference_no%20FROM%20client_suiting%20WHERE%20client_name%20=%20%277489%27
This part of a hook after insert in an APPGini generated script.
An associative array where the keys are field names and the values are the field data values that were inserted into the new record.
* For this table, the array items are:
* $data['issue_date'], $data['client_name'], $data['suiting_ref'], $data['inventory_item'], $data['inventory_qty'], $data['inventory_item_size'], $data['inventory_size_category'], $data['inventory_cost'], $data['inventory_value']
Now that I look at it again an easy solution would be if $client= $data['suiting_ref'] or however one should compose it.
*
How do you get the id of the client? From a query or a form? Hovewer, try:
header("Location:inventory_issue_view.php?filterer_client_name=$clientId");
where $clientId is a variable containing the number.
try this
header("Location:inventory_issue_view.php?filterer_client_name=".$clientId.");
or
header("Location:inventory_issue_view.php?filterer_client_name=$clientId");

PHP PDO get all row names as objects containing value

I have a mySQL database table setup called site.
Rather than qet the column headings to produce a PHP object of values I want to produce data from the rows.
TABLE: site
:::field:::::value:::::
url www.example.com
name example site
etc Example Etcetera
So I want to be able to get this information from the server by calling the column name I want and the row I am after. I want to do this for all fields in site as I don't want to do multiple calls for the various different rows in site; I'd rather store all the information from the beginning in the object:
eg. <? echo $site['url']; ?>
I created this put it appears to be causing an error:.
$sql = "SELECT * FROM `site`";
$site[];
foreach($sodh->query($sql) as $sitefield){
$site[$sitefield['field']] = $sitefield['value'];
}
Obviously I've missed something. ¿Any idea as to what?
$site[];
should be
$site = array();

add a name value pair to posted data for joomla to consume and insert into DB

I'm trying to use the Joomla framework to make a create in the main content table.[http://docs.joomla.org/How_to_use_the_JTable_class] This works fine except that some data comes from posted variables and some from logic that happens when a file is uploaded moments before (store the random image name of a jpg)
$data=&JRequest::get('post');
this takes a ref to the posted values and I want to add to this Array or Object my field. The code I have makes the new record but the column images, doesnt get my string inserted.
I am trying to do something like$data=&JRequest::get('post');
$newdata=(array)$data;
array_push($newdata,"images"=>"Dog");
i make newdata as data is a ref to the posted variables and i suspect wont there fore allow me to add values to $data.
I'm a flash guy normally not a php and my knowledge is letting me down here.
Thanks for any help
Right, first thing:
$data=&JRequest::get('post');
$data is an array, you do not have to cast it. To add another element to the array as described in the comments do this:
$data['images'] = 'cats';
If you are using normal SQL to do the insert then you would do something like this to get the last inserted id e.g. the id of the row you just inserted:
$db = $this->getDBO();
$query = 'Some sql';
$db->setQuery($query);
if (!$db->query()) {
JError::raiseWarning(100, 'Insert failed - '.$db->getErrorMsg());
}
$id = $db->insertid();
If you are developing in Joomla I suggest you use the db functions provided to you rather than mysql_insert_id();
[EDIT]
If you want to use store then you can get the last inserted id like so:
$row->bind($data);
$row->check();
$row->store();
$lastId = $row->id;

Categories