Dynamic multi-diemnsional array sorting - php

Hello everyone I have a simple question. How to choose by string which column of multidimensional array to sort. Here is some informations you should know.
I have table with eshops data and a function which shows me orders for particular eshop like this
$sql = mysql_query("SELECT * FROM eshops WHERE active = 1");
while($data = mysql_fetch_assoc($sql)){
$res = showOrders($data['host'],$data['user'],$data['pass'],$data['db'],$data['prefix']);
echo $res;
}
This was my first try how to show all orders i made filters by passing sql condition to my function. But then i wanted to sort the result, and Here comes the problem i can sort every eshop but i want to sort all eshops at once it means that if i sort them by order date the eshops mix up but that is not possible right now so i made this.
Example:
$GLOBALS['container'] = array();
and in a function i put datas to array like this
$GLOBALS['container'][] = array($data['eshop_id'],$data2['order_id']
,$data2['order_status'],$data2['order_number'],$data2['d_f_name'],$data2['d_l_name']
,$data2['order_total'],$data2['payment_method_id'],$data2['shipping_method_id']
,$data2['Exportovano'],$data2['C_Baliku'],date("d.m.Y",strtotime($data2['order_date'])));
Then i sort i like that
foreach($GLOBALS['container'] as $key => $datas){
$eshop_id[$key] = $datas[0];
$order_id[$key] = $datas[1];
$order_st[$key] = $datas[2];
$order_nm[$key] = $datas[3];
$d_f_name[$key] = $datas[4];
$d_l_name[$key] = $datas[5];
$order_tl[$key] = $datas[6];
$pay_m_id[$key] = $datas[7];
$shp_m_id[$key] = $datas[8];
$exported[$key] = $datas[9];
$parc_num[$key] = $datas[10];
$ord_date[$key] = $datas[11];
}
array_multisort($ord_date, SORT_DESC,$d_f_name, SORT_ASC, $GLOBALS['container']);
Here we go now i can sort as i wish then by foreach container select order by order also adding a condition but how to choose what i want to sort? P.e. i pass data by get
?sort=ord_date&orn=asc
I want sort ord_date variable ascending how? PLease help me thanks.
I am also open for other solutions.

It will be easier to write in SQL query, just after WHERE statement use a ORDERED BY (column) statement.
EDIT:
Using ORDER BY statement

Related

perform two different fetch on the same query result

I have this query:
$all = $dbh->query("SELECT DISTINCT movimenti.nome, SUM(movimenti_carta.importo)
FROM movimenti_carta JOIN movimenti ON movimenti_carta.movimento=movimenti.id
WHERE month(data)='$mese' AND year(data)='$anno' GROUP BY movimenti.nome");
It retrieves two columns from my db. What I want to do is to put each column in a separate array.
If I do:
$labels=$all->fetchAll(PDO::FETCH_COLUMN,0);
I get the values for the first column with the format I am looking for.
I tried to do:
$values=$all->fetchAll(PDO::FETCH_COLUMN,1);
after the first fetch but $all is unset by the first fetch and the result is that $values is an empty array (I was pretty sure of this but did give it a try).
I can build the arrays in php after I fetch an array with both columns but I was wondering how to get my goal using PDO api.
Of course it will never work this way, as fetchAll() returns data only once.
The only idea I could think of is PDO::FETCH_KEY_PAIR constant:
$data = $all->fetchAll(PDO::FETCH_KEY_PAIR);
$labels = array_keys($data);
$values = array_values($data);
It feels like you are overcomplicating this. It's easily done in PHP, and if you just use PDOStatement::fetch, you don't need to worry about array manipulation; it will also be more friendly to your memory usage if you have a lot of data.
$labels = [];
$values = [];
while ($row = $all->fetch(PDO::FETCH_NUM)) {
$labels[] = $row[0];
$values[] = $row[1];
}
Maybe you could use array_column.
$all = $dbh->query("
SELECT DISTINCT movimenti.nome, SUM(movimenti_carta.importo) AS importo
FROM movimenti_carta
JOIN movimenti ON movimenti_carta.movimento=movimenti.id
WHERE month(data)='$mese'
AND year(data)='$anno'
GROUP BY movimenti.nome
");
// set of results indexed by column name
$results = $all->fetchAll(PDO::FETCH_ASSOC);
// set of values from the column 'nome'
$nomes = array_column($results, 'nome');
// set of values from the column 'importo'
$importos = array_column($results, 'importo');

Implode ids within a while loop and add commas between

I'm trying to display some sub-content so I need to get the child id's of their parent.
This is how I wanted to do it:
$ids = "SELECT * FROM `web_categories` WHERE parent_id = 14 AND published = 1";
$idscon = $conn->query($ids);
while ($ids = $idscon->fetch_array()){
$idss .= $ids['id'];
}
$project1 = "SELECT * FROM `web_content` WHERE catid in ('$idss') AND state = 1";
$projectcon1 = $conn->query($project1);
$projectcr1 = array();
while ($projectcr1[] = $projectcon1->fetch_array());
I tried imploding $idss like this:
$implode = implode(',', $idss);
But this gives me Invalid arguments passed. What am I doing wrong?
You are doing wrong in the first while loop.
Here it is, $idss .= $ids['id'];, You are doing wrong this. You are storing value in a variable as string, but when you try to implode this... It throws an error!!! Cause implode() use to make array into string. So follow the below steps.
Change $idss .= $ids['id']; to $idss[] = $ids['id']; and instead of implode() use the join().
Create an array names $idss, and push or insert the ids into that array. Then implode/join that IDs.
$idss = array();
while ($ids = $idscon->fetch_array()){
$idss[] = $ids['id'];
}
$implode = implode(',', $idss);
Now you can use this $implode variable in the next query.
You just need to store all IDs in an array, something like $yourIDs[] = $ids['id']; inside your first while loop.
With $idss .= $ids['id']; you can't use implode() because result of this action should be something like "1234" without any single comma.
You just need to use like that:
<?php
$yourIDs = array();
while ($ids = $idscon->fetch_array()){
$yourIDs[] = $ids['id']; // this will save all IDs into $yourIDs array.
}
$idss = implode(',',$yourIDs); // result of this should be 1,2,3,4 etc.
?>
I think you could most likely do that in one query
SELECT * FROM `web_content` WHERE `catid` in (
SELECT `id` FROM `web_categories` WHERE `parent_id` = 14 AND `published` = 1
) AND `state` = 1
The original code was, in effect, doing exactly this but in two stages - sub-queries can be slow sometimes but it does depend on the size of the db and the number of expected results. It is true that joins can be and generally are more efficient than dependant subqueries so perhaps that is another option to explore. As mysql executes the query plan from the outside to the inside rather than the intuitive inside to outside subqueries can cause the server to crawl - it really depends on the complexity of the subquery and the amount of data that has to be processed.

Preserve the insert order in a PHP array

I have a PHP page used by an Ajax call that I want to return an ordered list of items as JSON. However whichever field I use in the query's 'order by' clause the array is always ordered by its key, the ID field.
Is there a way to preserve the insert order of each item in the PHP array?
This is the code that generates the JSON array:
$Soggetti = array();
while($row = $db->fetch_array($query))
{
$Soggetti[$row['ID']] = array();
$Soggetti[$row['ID']]['Denominazione'] = $row['Denominazione'];
$Soggetti[$row['ID']]['Indirizzo'] = $row['Indirizzo'].','.$row['Comune'].', '.$row['Provincia'];
$Soggetti[$row['ID']]['Superficie'] = $row['Superficie'];
$Soggetti[$row['ID']]['Lat'] = $row['Lat'];
$Soggetti[$row['ID']]['Lng'] = $row['Lng'];
$Soggetti[$row['ID']]['Tipologia'] = $row['Tipologia'];
$Soggetti[$row['ID']]['CodiceIscrizione'] = $row['CodiceIscrizione'];
$Soggetti[$row['ID']]['Visitato'] = intval($row['Visitato']);
}
echo json_encode($Soggetti)
If the problem lies in where the JSON is interpreted, in the client, you can use this syntax to return a JSON array (enclosed in []) instead of a JSON object (enclosed in {}).
echo json_encode(array_values($Soggetti));
You are building a 2 dimensional array with the key on first dimension being the id. Assuming id is numeric you will end up with nested objects in JSON, with the key of top level being the numerical id values. The problem here being that object properties have no guarantee of order.
To get the order of the query results, simply build a 1-D array inside your result set loop like this:
$tempArray = array();
$tempArray['ID'] = $row['ID'];
$tempArray['Denominazione'] = $row['Denominazione'];
$tempArray['Indirizzo'] = $row['Indirizzo'].','.$row['Comune'].', '.$row['Provincia'];
$tempArray['Superficie'] = $row['Superficie'];
$tempArray['Lat'] = $row['Lat'];
$tempArray['Lng'] = $row['Lng'];
$tempArray['Tipologia'] = $row['Tipologia'];
$tempArray['CodiceIscrizione'] = $row['CodiceIscrizione'];
$tempArray['Visitato'] = intval($row['Visitato']);
$Soggetti[] = $tempArray;
The will encode to JSON as an array of objects with the array numerically indexed from 0 based on order in result set.
You need an ORDER BY clause of your MySQL statement.
In this case you want to order by your id, and in ascending order, so;
SELECT * from your_table ORDER BY id ASC
Reference: MySQL Sorting
The problem may lie in your mysql query, try:
select * from table_name order by ID asc

How do I insert values into an multidimensional-array, then show them?

I'm fairly new to php, and I don't know how to work with arrays very well. Here's the deal, I want to add into a multidimensional array three or more values I obtain from my database, then I want to sort them based on the timestamp (one of the values). After that, I want to show all of the sorted values. I can't seem to do this, here's the code
$queryWaitingPatients = 'SELECT ArrivalTime, TargetTime, Order, Classification FROM exams WHERE (CurrentState = "Pending")';
$results = mysql_query($queryWaitingPatients) or die(mysql_error());
if (mysql_num_rows($results) == 0) {
echo '<p>There\'s currently no patient on the waiting list.</p>';
return;
}
while ($rows = mysql_fetch_array($results)) {
extract($rows);
//now is the part that I don't know, putting the values into an array
}
// I'm also not sure how to sort this according to my $TargetTime
asort($sortedTimes);
//the other part I don't know, showing the values,
Thanks for the help!
Well, let's look at your code. First, you have a query that's returning a result set. I don't recommend using mysql_fetch_array because it's not only deprecated (use mysqli functions instead) but it tends to lend itself to bad code. It's hard to figure out what you're referencing when all your keys are numbers. So I recommend mysqli_fetch_assoc (be sure you're fully switched to the mysqli functions first, like mysql_connect and mysqli_query)
Second, I really dislike using extract. We need to work with the array directly. Here's how we do this
$myarray = array();
while ($rows = mysqlI_fetch_assoc($results)) {
$myarray[] = $rows;
}
echo $myarray[0]['ArrivalTime'];
So let's go over this. First, we're building an array of arrays. So we initialize our overall array. Then we want to push the rows onto this array. That's what $myarray[] does. Finally, the array we're pushing is associative, meaning all the keys of the row match up with the field names of your query.
Now, the sorting really needs to be done in your query. So let's tweak your query
$queryWaitingPatients = 'SELECT ArrivalTime, TargetTime, `Order`, Classification
FROM exams
WHERE CurrentState = "Pending"
ORDER BY TargetTime';
This way, when your PHP runs, your database now churns them out in the correct order for your array. No sorting code needed.
$arr = array();
while ($rows = mysql_fetch_array($results)) {
array_push ($arr, $row);
}
print_r($arr);
<?php
$queryWaitingPatients = ' SELECT ArrivalTime, TargetTime, Order, Classification, CurrentState
FROM exams
WHERE CurrentState = "Pending"
ORDER BY TargetTime ';
$results = mysql_query($queryWaitingPatients) or die(mysql_error());
if ($results -> num_rows < 1)
{
echo '<p>There\'s currently no patient on the waiting list.</p>';
}
else
{
while ($rows = mysqli_fetch_array($results))
{
$arrivaltime = $row['ArrivalTime'];
$targettime = $row['targettime'];
$order = $row['Order'];
$classification = $row['Classification'];
echo "Arrival: ".$arrivaltime."--Target time: ".$targettime."--Order: ".$order."--Classification: ".$classification;
}
}
echo "Done!";
//or you could put it in a json array and pass it to client side.
?>

php & mysql - loop through columns of a single row and passing values into array

I have a mysql table with columns id, f1, f2, f3, ..., f20 where id is productID and f1,...f20 are product features. Depending on each product, some might have all, none or only some columns filled.
Each column holds a delimited string like a#b#c#d where a,b,c,d are values in different languages (a=english, b=french etc)
I need to select a row by it's id, explode each column's value (f1,f2...) with '#' in order to get the language part I need and then pass the values to an array in order to use in my product spec page.
How do I loop through the fetched row (i'm using $row = my_fetch_array) and put the exploded value into a one dimension array like $specs=('green', 'M', '100', 'kids'...) etc?
PS:I know, is complicated but I cant come up with a better idea right now.
Try this:
$result = mysql_query("...");
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
$arr = array();
foreach ($row as $k=>$v)
{
$features = explode("#", $v);
$value = $features[1]; // get the specific language feature
$arr[] = $value;
}
$specs = join(", " , $arr);
}
Not sure this is the best way togo but you could define an array with your langs, then access the result by lang
<?php
$langs=array('eng'=>0,'fr'=>1,'ger'=>2,'geek'=>3);
while ($row=mysql_fetch_assoc($result)) {
$specs=explode('#',$row['f1']);
$other=explode('#',$row['f2']);
...
}
//Get lang from cookie that you could set elsewhere
$lang=(isset($_COOKIE['lang']))?$_COOKIE['lang']:'eng';
echo $specs[$langs[$lang]];
?>
My solution for how I understand you question:
// Make a MySQL Connection
$sQuery = "SELECT f1,f2,... FROM table WHERE id = ...";
$oResult = mysql_query($sQuery) or die(mysql_error());
//Fetch assoc to use the column names.
$aRow = mysql_fetch_assoc($oResult);
//Prepare the product properties array
$aProductProperties = array("English"=>array(),"French"=>array(),"Dutch"=>array());
//Loop over all the columns in the row
foreach($aRow as $sColName=>$sColVal){
//Explde the column value
$aExplodedCol = explode("#",$sColVal);
//The code below could be nicer when turned into a looped that looped over every language,
//But that would make the code less readable
$aProductProperties['English'][$sColName] = $aExplodedCol[0];
$aProductProperties['French'][$sColName] = $aExplodedCol[1];
$aProductProperties['Dutch'][$sColName] = $aExplodedCol[2];
}
//Done, you should now have an array with all the product properties in every language

Categories