I'm writing some data off my MySQL database into an array using columns with php like this:
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$row->id = $row["ID"];
$row->product = $row["product"];
$row->aantal = $row["Aantal"];
$row->price = $row["Price"];
$od[] = $row;
}
} else {
echo "0 results";
}
now later I have to use the $row->id and $row->product of the different columns seperatly to get some more data out of another table in MySQL. I've been trying to accomplish this:
$to = 0;
foreach($od as $odt)
{
$odb= $odt[$to]["ID"];
$sql = "SELECT `Name` FROM `detail` WHERE `ID` = '$odb'";
$to++;
But this doesn't seem to work, I've tried dozen of others but can't seem to get this thing right...
Any solutions or remarks?
EDIT:
Array ( [0] => Array ( [ID] => 3 [product] => 10 [Aantal] => 1 [Price] => 3 ) [1] => Array ( [ID] => 4 [product] => 13 [Aantal] => 1 [Price] => 3 ) [2] => Array ( [ID] => 5 [product] => 3 [Aantal] => 3 [Price] => 4 ) )
You are doing it wrong, you are saving object in an array and trying the get the data with array, it should be as
foreach($od as $key=>$odt)
{
$odb= $odt->id;
$sql = "SELECT `Name` FROM `detail` WHERE `ID` = '$odb'";
}
Related
I need to make my array better.
I am getting data from database and i have milestones and milestone_parts. i want two-dimensional array. I need data of milestones in the first dimension and milestone_parts in the second dimension.
With this code:
$query = "
SELECT
a.id AS `milestone_id`,
a.titel AS `milestone_titel`,
a.client AS `client`,
a.verkocht_id AS `milestone_verkocht_id`,
b.id AS `milestonefase_id`,
b.titel AS `milestonefase_titel`,
b.milestone_id AS `milestonefase_milestone_id`,
b.omschrijving AS `milestonefase_omschrijving`
FROM `milestones` a
INNER JOIN `milestone_parts` b ON a.id=b.milestone_id
WHERE a.verkocht_id = '99'
";
$result= $db->query($dbh, $query);
while ($row = $db->fetchassoc($result))
{
$stone = array($row['milestone_verkocht_id'], $row['milestone_id'], $row['milestone_titel'], $row['client']);
$fase = array($row['milestonefase_milestone_id'],$row['milestonefase_id'],$row['milestonefase_titel']);
$stone[] = $fase;
echo '<pre>'; print_r($stone); echo '</pre>';
}
I get this as result
Array
(
[0] => 99
[1] => 6
[2] => string
[3] => string
[4] => Array
(
[0] => 6
[1] => 10
[2] => string
)
)
Array
(
[0] => 99
[1] => 6
[2] => string
[3] => string
[4] => Array
(
[0] => 6
[1] => 11
[2] => string
)
)
but I need (with names) this:
Array
(
[milestone_verkocht_id] => 99 // This is project id
[milestone_id] => 6
[milestone_title] => string
[client] => string
[10] => Array
(
[milestonefase_milestone_id] => 6
[milestonefase_id] => 10
[milestone_title] => string
)
[11] => Array
(
[milestonefase_milestone_id] => 6
[milestonefase_id] => 11
[milestone_title] => string
)
[12] => Array
(
[milestonefase_milestone_id] => 6
[milestonefase_id] => 12
[milestone_title] => string
)
)
Can you help me or do you have a solution? Help me please!
you can cycle each field returned by the query, checking the field name and making new arrays
$stones = array();
while ($row = $db->fetchassoc($result)) {
$fase = array();
$stone = array('milestones' => array());
foreach ($row as $k => $v) {
if (strpos($k, 'milestonefase_') === 0) {
$fase[$k] = $v;
} else {
$stone[$k] = $v;
}
}
if(!isset($stones[$stone['milestone_id']])) {
$stones[$stone['milestone_id']] = $stone;
}
$stones[$stone['milestone_id']]['milestones'][$fase['milestonefase_id']] = $fase;
}
echo '<pre>'.print_r($stones, true).'</pre>';
Edit
made some changes in order to match the request. Now we use $stones to store the information we already have on a milestone, adding to it the different "$fase" returned from the query
Probably a more clean way is to retrieve all the information with two different queries one for milestones and the other for the fases
Edit2
Added a sub-array for the milestone fases
I am looking to group my array into sub arrays when they have the same Supplier ID. I have looked at many similar questions on here but I cannot get any of them to work for me. I have tried foreach loops and even the PHP merge function. I have to use MySQLi for this so I cannot group using PDO.
This is what I have:
Array
(
[supplierID] => 1
[drugID] => 1
[costPrice] => 9.98
[reorderQTY] => 50
)
Array
(
[supplierID] => 1
[drugID] => 2
[costPrice] => 9.98
[reorderQTY] => 50
)
Array
(
[supplierID] => 2
[drugID] => 3
[costPrice] => 9.98
[reorderQTY] => 50
)
This is what I need:
Array
(
[supplierID] => 1
Array
(
[drugID] => 1
[costPrice] => 9.98
[reorderQTY] => 50
)
[drugID] => 2
[costPrice] => 9.98
[reorderQTY] => 50
)
)
Array
(
[supplierID] => 2
Array
(
[drugID] => 3
[costPrice] => 9.98
[reorderQTY] => 50
)
)
This is the PHP where I am outputting the array to get the above result:
if(isset($_POST["automatic"])){
$sql = "SELECT supplierID,drugID,costPrice,reorderQTY FROM drugs WHERE quantityInStock <= reorderLevel";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "<pre>";
print_r($row);// I need to sort this array into sub array groups by supplierID key
echo "</pre>";
}
}else{
echo " <tr><td>0 results </td></tr>";
}
mysqli_close($conn);
}
Something like this will do it,
$out = [];
while($row = mysqli_fetch_assoc($result)) {
$out[$row->supplierID][] = $row;
}
Basically I use the supplier id to create an array key, then assign an empty array. Then add all rows with that supplier id to that array.
This will create a multidimensional array.
I am getting array after loping
while($num_rows1 = mysql_fetch_array($data_query_details1))
Now I want to check if my specific user_id ( suppose here user_id=3) stay 2/3 ( my specific value) times same date in array then print ok otherwise print need more.
Array
(
[0] => 3
[user_id] => 3
[1] => 2014-07-18
[date(FROM_UNIXTIME(date))] => 2014-07-18
)
Array
(
[0] => 3
[user_id] => 3
[1] => 2014-07-18
[date(FROM_UNIXTIME(date))] => 2014-07-18
)
Array
(
[0] => 4
[user_id] => 4
[1] => 2014-07-18
[date(FROM_UNIXTIME(date))] => 2014-07-18
)
any logic ?
$dates = array();
$stay = false;
$userid_to_check = 3;
while($row = mysql_fetch_array($data_query_details1)){
if($row['user_id']==$userid_to_check){
if(in_array($row[1], $dates)){
$stay = true;
break;
}
$dates[] = $row[1];
}
}
if($stay)echo 'ok';
else echo 'need more';
It's better to go with a more direct database select like the answers above though.
For example, what codebird mentioned:
$query = "select count(*) from your_table GROUP BY user_id HAVING date(FROM_UNIXTIME(date))='2014-07-18'";
$result = mysql_query($query);
$count = mysql_num_rows($result);
if($count>=2)echo 'ok';
else echo 'need more';
This should solve your issue:
SELECT user_id, COUNT(*) FROM your_table
WHERE DATE(FROM_UNIXTIME(date))='2014-07-18'
GROUP BY user_id
I have this query for mysql:
SELECT HOUR(time),COUNT(*) FROM pageview WHERE time >= DATE_SUB(NOW(),INTERVAL 12 HOUR) GROUP BY HOUR(time)
For example, this is the output:
Array
(
[0] => Array
(
[HOUR(time)] => 1
[COUNT(*)] => 1
)
[1] => Array
(
[HOUR(time)] => 10
[COUNT(*)] => 4
)
[2] => Array
(
[HOUR(time)] => 11
[COUNT(*)] => 5
)
)
However I want the output like this
Array
(
[1] => 1
[10] => 4
[11] => 5
)
The array index should be the value of [HOUR(time)].
I would prefer directly by changing the query.
To fetch the data I use this:
$stmt = $db->prepare($query);
$result = $stmt->execute();
$views = $stmt->fetchAll();
indexes in mysql results create automatically , to achieve your goal loop through your first array and create new one as you want
$result = array();
foreach($views as $row) {
$result[$row['HOUR(time)']] = array( 'COUNT(*)' => $row['COUNT(*)']);
}
print_r($result); // check output
or simpler form
$result = array();
foreach($views as $row) {
$result[$row['HOUR(time)']] = $row['COUNT(*)'];
}
print_r($result); // check output
Try with this
$res = array();
foreach($views as $data)
{
$res[$data['HOUR(time)']] = array( 'COUNT(*)' => $data['COUNT(*)']);
}
print_r($res);
I'm using Wordpress for this, but it is not a Wordpress-centric issue, so I am asking here.
You can see a snippet of my code here: http://pastebin.com/Cbc8wKvB
<?php
function getFormIds()
{
global $wpdb;
$sql = "
SELECT a.id
FROM wp_rg_lead a
WHERE a.form_id = 10 AND a.payment_status = 'Approved'
";
$query = $wpdb->get_results($sql);
if($query)
return $query;
return false;
}
function getFormInfo($form_id)
{
global $wpdb;
$sql = "
SELECT *
FROM wp_rg_lead_detail
WHERE lead_id = $form_id
";
$query = $wpdb->get_results($sql);
if($query)
return $query;
return false;
}
$credit_card_form_ids = getFormIds();
if ( $credit_card_form_ids ) {
$entries = array();
foreach( $credit_card_form_ids as $entry) {
$single_entry = getFormInfo($entry->id);
if ( $single_entry ) {
$entry_array = array();
$full_array = array();
foreach( $single_entry as $entry ) {
$curr_array = array( $entry->field_number => strip_tags($entry->value));
$entry_array[$entry->field_number] = strip_tags($entry->value);
array_push($full_array, $entry_array);
}
}
}
#var_dump('full_array', $full_array);
}
Basically I'm running a DB query to grab all entry ID's that match a certain criterion. This returns an array of objects of single values. I then pass this array through a foreach() and try to extract/combine data to make it easier to work with.
Here is a print_r() of a $single_entry from the code above: http://pastebin.com/RZmfD2EU
single entry:
Array
(
[0] => stdClass Object
(
[id] => 1983
[lead_id] => 86
[form_id] => 10
[field_number] => 34
[value] => 695
)
[1] => stdClass Object
(
[id] => 1982
[lead_id] => 86
[form_id] => 10
[field_number] => 39
[value] => 0
)
[2] => stdClass Object
(
[id] => 1981
[lead_id] => 86
[form_id] => 10
[field_number] => 40.1
[value] => Yes
)
... etc etc.
With the code I provided, the final array seems to collapse somehow -- meaning I lose entries somewhere along the way.
Ideally I'd love the data to associate id's to their key => value pairs. Namely:
[id] => [lead_id]
[field_number#] => [field_number_value]
For a actual representation of the first few lines of data provided:
Array (
[0] =>
(
[id] => 86
[34] => 695
[39] => 0
[40.1] => Yes
...etc
)
)
Is there a better way to loop through and associate data than what I'm doing? I'm relatively new/bad at PHP and MySQL and would love some guidance.
I'm not sure if I'm understanding it right but if you want to assign values to specified keys…
if ( $single_entry ) {
$entry_array = array();
$full_array = array();
foreach( $single_entry as $entry ) {
$curr_array = array( $entry->field_number => strip_tags($entry->value));
$entry_array[$entry->field_number] = strip_tags($entry->value);
$lead_id = $entry['lead_id'];
$full_array[$lead_id] = $entry_array;
}
}