I'm struggling, creating a mysql query for the following scenario:
Array
(
[0] => stdClass Object
(
[id] => 1
[qid_id] => 1
[user_id] => 1
[question] => Favorite Place
[answer] => New York
[username] => john
)
[1] => stdClass Object
(
[id] => 2
[qid_id] => 1
[user_id] => 1
[question] => Additional Info
[answer] => N/A
[username] => john
)
[2] => stdClass Object
(
[id] => 3
[qid_id] => 1
[user_id] => 1
[question] => Favorite Place
[answer] => London
[username] => peter
)
[3] => stdClass Object
(
[id] => 4
[qid_id] => 1
[user_id] => 1
[question] => Additional Info
[answer] => N/A
[username] => peter
)
[4] => stdClass Object
(
[id] => 5
[qid_id] => 1
[user_id] => 2
[question] => Favorite Place
[answer] => Sidney
[username] => mike
)
[5] => stdClass Object
(
[id] => 6
[qid_id] => 1
[user_id] => 2
[question] => Additional Info
[answer] => N/A
[username] => mike
)
)
I'm trying to display data in html table, and so far I got the following:
$sql = "SELECT DISTINCT question"
. "\n FROM faq "
. "\n WHERE qid_id = 1";
$data = $db->fetchAll($sql);
$sql2 = "SELECT f.*, u.username"
. "\n FROM faq as f"
. "\n LEFT JOIN users as u on u.id = f.user_id "
. "\n WHERE f.qid_id = 1";
$data2 = $db->fetchAll($sql2);
<table>
<thead>
<tr>
<?php
print "<th>USERNAME</th>";
for ($i = 0; $i < count($data); $i++) {
print "<th>" . $data[$i]->question . "</th>";
}
?>
</tr>
</thead>
<tbody>
The above works fine, I'm having problem displaying the rest of the table, using second query ($data2). I'm not sure how to group fields in order to get unique results for each row.
The idea is to have something like:
USERNAME Favorite Place Additional Info
john New York N/A
peter London N/A
mike Sidney N/A
Related
I have a record of 100 or 200 questions when user submit the test all 100 or 200 questions from caches need to save to MySQL. Right now I have to loop in PHP and every time a single query has executed, any idea how I send an array in MySQL procedure and the loop of 100 or 200 should be done in MySQL procedure. I can convert the array to JSON as JSON is supported in MySQL.
array which needs to be insert looks like this
Array
(
[0] => Array
(
[0] => 19287
[1] => 1
[2] => 1
[3] => 101
[4] => 224
[5] => 0
)
[1] => Array
(
[0] => 19285
[1] => 0
[2] => 1
[3] => 101
[4] => 2
[5] => 0
)
[2] => Array
(
[0] => 19289
[1] => 1
[2] => 1
[3] => 72
[4] => 23
[5] => 0
)
[3] => Array
(
[0] => 19290
[1] =>
[2] => 1
[3] => 106
[4] => 3
[5] => 0
)
[4] => Array
(
[0] => 19291
[1] => 0
[2] => 1
[3] => 230
[4] => 76
[5] => 0
)
)
You can build a multi-row INSERT in your PHP code. Assuming your data is in $arr:
$sql = "INSERT INTO yourtable (...column list...) VALUES ";
$vsets = array();
foreach ($arr as $values) {
$vsets[] = str_replace(array(',,', ',,'), ',NULL,', "(" . implode(',', $values) . ")");
}
$sql .= implode(',', $vsets);
echo $sql;
Output:
INSERT INTO yourtable (...column list...)
VALUES (19287,1,1,101,224,0),(19285,0,1,101,2,0),
(19289,1,1,72,23,0),(19290,NULL,1,106,3,0),
(19291,0,1,230,76,0)
Make sure you filter, validate or cast your data, to prevent any non-integers (or any SQL) from being injected into your queries.
This question already has answers here:
How to count array with group by
(2 answers)
Closed 4 months ago.
I am creating a notification system. That results shown below returns all notifications for a particular user. I want to group the array by 'notify_id' and also count each item in a group.
Such that I'll have :
"Jack Bill is now following you", John Doe commented on your post 2",
"John Doe and 2 others commented on your post 1"
Array ( [0] =>
Array ( [id] => 1
[user_id] => 10
[type] => follow
[notify_id] => 13
[notify_date] => 1483444712
[firstname] => Jack
[surname] => Bill
[picture] => )
[1] =>
Array ( [id] => 10
[user_id] => 10
[type] => comment
[notify_id] => 2
[notify_date] => 1482159309
[firstname] => John
[surname] => Doe
[picture] => )
[2] =>
Array ( [id] => 8
[user_id] => 10
[type] => comment
[notify_id] => 1
[notify_date] => 1482159219
[firstname] => John
[surname] => Doe
[picture] => )
[3] =>
Array ( [id] => 6
[user_id] => 16
[type] => comment
[notify_id] => 1
[notify_date] => 1482159129
[firstname] => James
[surname] => Canon
[picture] => )
[4] =>
Array ( [id] => 5
[user_id] => 14
[type] => comment
[notify_id] => 1
[notify_date] => 1482159079
[firstname] => Sharon
[surname] => Abba
[picture] => ) )
You have two questions, I will try to answer both of them:
I want to group the array by 'notify_id'...
Assuming your array name is $a:
$return = array();
foreach($a as $val) {
if (!isset($return[$val['notify_id']])) {
$return[$val['notify_id']] = array();
}
$return[$val['notify_id']][] = $val;
}
print_r($return); // <-- Your array is grouped by notify_id
... and also count each item in a group.
Now you have your grouped by notify_id in $return so:
foreach($return as $k => $v) {
echo count($v) . ' values are present for notify #' . $k;
// It will display something like: 10 values are present for notify #1
}
Hope it will help, good luck!
I am decoding JSON from an API:
$api = "http://xyzdomain.com/api";
$json = file_get_contents($api);
$array = json_decode($json,true);
print_r($array);
Here is the sample result of API objects and arrays.
Array
(
[status] => ok
[count] => 1
[meta] => Array
(
[count] => 1
)
[data] => Array
(
[accountid] => Array
(
[0] => Array
(
[all] => Array
(
[fans] => 16
[user post] => 333
[user Details] => xyz
)
[scorelist] => 1
[name] => John Doe 1
[timespent] => 5887
[nation] => usa
)
[1] => Array
(
[all] => Array
(
[fans] => 123
[user post] => 903
[user Details] => mno
)
[scorelist] => 6
[name] => John Doe 2
[timespent] => 1269
[nation] => usa
)
[2] => Array
(
[all] => Array
(
[fans] => 16
[user post] => 303
[user Details] => abc
)
[scorelist] => 1
[name] => John Doe 3
[timespent] => 9292
[nation] => ussr
)
[3] => Array
(
[all] => Array
(
[fans] => 16
[user post] => 333
[user Details] => jqr
)
[scorelist] => 1
[name] => John Doe 4
[timespent] => 75600
[nation] => usa
)
)
)
)
I used only three samples above, but it may go up to 500 or more.
I have basically two questions here:
I want to make a table using jqgrid or datatables or any other way it looks good.
How can I sum timespent where nation is, say, USA in above example?
$timespent_usa = 0;
foreach($array['data']['accountid'] as $account) {
if ($account['nation'] == 'usa') {
$timespent_usa += $account['timespent'];
}
}
echo $timespent_usa;
I'm not sure on how you plan on getting the data into datatables, you could just echo the data within each $account array within the foreach loop into <td> elements.
I have an SQL query that returns 14 rows per userID.
It pulls a lot of data to verify different fields, but specifically returned I need the first and last name. The first name I can get from $firstName[]=$row2[0]; but I can't get the last name value out of the array. I can see its position, but I'm not sure how to call that position in the loop.
echo '<table border=1><tr><td>Student ID</td><td>Full Name</td><td>Class</td></tr>';
//Build Student Info1 table
foreach($userID as $val) {
$user_id= $val;
$query="SELECT * FROM wp_usermeta WHERE meta_key ='purchased_instances' AND user_id ='$user_id'";
$query2="SELECT meta_value FROM wp_usermeta WHERE user_id ='$user_id'";
$result = $link->query($query)or die($link->error);
$result2 = $link->query($query2)or die($link->error);
if($result->num_rows > 0){
/* fetch associative array */
$i=0;
while ($row = mysqli_fetch_array($result)) {
while($row2 = mysqli_fetch_array($result2)) {
$details[$i][]=$row2;
$firstName[]=$row2[0];
$lastName[]=$row2[0]; //Can't get this value right, need the first element of the 2 indexed array
}
$result1[]=$row;
$studentID[$i]=$row[1];
$classDetails[$i]=$row[3];
echo'<tr><td>';
print_r($studentID[$i]);
echo '</td><td>';
print_r($firstName[$i]);
echo ' ';
print_r($lastName[$i]);
echo'</td><td>';
print_r($classDetails[$i]);
echo '</td></tr>';
$i++;
}
/* free result set */
mysqli_free_result($result);
}
}
echo '</table>';
This is what print_r($details) produces:
Array ( [0] => Array ( [0] => 2567 [umeta_id] => 2567 [1] => 185 [user_id] => 185 [2] => first_name [meta_key] => first_name [3] => Kevin [meta_value] => Kevin ) [1] => Array ( [0] => 2568 [umeta_id] => 2568 [1] => 185 [user_id] => 185 [2] => last_name [meta_key] => last_name [3] => RealLastNameHERE [meta_value] => RealLastNameHERE ) [2] => Array ( [0] => 2569 [umeta_id] => 2569 [1] => 185 [user_id] => 185 [2] => nickname [meta_key] => nickname [3] => KMOwner [meta_value] => KMOwner ) [3] => Array ( [0] => 2570 [umeta_id] => 2570 [1] => 185 [user_id] => 185 [2] => description [meta_key] => description [3] => [meta_value] => ) [4] => Array ( [0] => 2571 [umeta_id] => 2571 [1] => 185 [user_id] => 185 [2] => rich_editing [meta_key] => rich_editing [3] => true [meta_value] => true ) [5] => Array ( [0] => 2572 [umeta_id] => 2572 [1] => 185 [user_id] => 185 [2] => comment_shortcuts [meta_key] => comment_shortcuts [3] => false [meta_value] => false ) [6] => Array ( [0] => 2573 [umeta_id] => 2573 [1] => 185 [user_id] => 185 [2] => admin_color [meta_key] => admin_color [3] => fresh [meta_value] => fresh ) [7] => Array ( [0] => 2574 [umeta_id] => 2574 [1] => 185 [user_id] => 185 [2] => use_ssl [meta_key] => use_ssl [3] => 0 [meta_value] => 0 ) [8] => Array ( [0] => 2575 [umeta_id] => 2575 [1] => 185 [user_id] => 185 [2] => show_admin_bar_front [meta_key] => show_admin_bar_front [3] => true [meta_value] => true ) [9] => Array ( [0] => 2576 [umeta_id] => 2576 [1] => 185 [user_id] => 185 [2] => wp_capabilities [meta_key] => wp_capabilities [3] => a:1:{s:10:"subscriber";b:1;} [meta_value] => a:1:{s:10:"subscriber";b:1;} ) [10] => Array ( [0] => 2577 [umeta_id] => 2577 [1] => 185 [user_id] => 185 [2] => wp_user_level [meta_key] => wp_user_level [3] => 0 [meta_value] => 0 ) [11] => Array ( [0] => 2578 [umeta_id] => 2578 [1] => 185 [user_id] => 185 [2] => mgm_member_options [meta_key] => mgm_member_options [3] => a:35:{s:2:"id";i:185;s:13:"custom_fields";a:6:{s:10:"first_name";s:5:"Kevin";s:9:"last_name";s:13:"RealLastNameHERE";s:5:"email";s:23:"RealEmailAddressHERE";s:8:"username";s:7:"RealuserNameHERE";s:8:"password";s:16:"Lwp9E8RZECyB9bzb";s:13:"password_conf";N;}s:22:"other_membership_types";a:0:{}s:12:"payment_info";a:0:{}s:6:"coupon";a:2:{s:12:"update_usage";b:0;s:15:"coupon_usage_id";b:0;}s:7:"upgrade";a:1:{s:6:"coupon";a:0:{}}s:6:"extend";a:0:{}s:4:"code";s:10:"mgm_member";s:4:"name";s:10:"Member Lib";s:11:"description";s:10:"Member Lib";s:7:"setting";a:0:{}s:6:"saving";b:1;s:8:"trial_on";i:0;s:10:"trial_cost";d:0;s:14:"trial_duration";i:0;s:19:"trial_duration_type";s:1:"d";s:16:"trial_num_cycles";i:0;s:8:"duration";i:1;s:13:"duration_type";s:1:"l";s:6:"amount";d:0;s:8:"currency";s:3:"USD";s:9:"join_date";i:1383183050;s:13:"last_pay_date";s:0:"";s:11:"expire_date";s:0:"";s:15:"membership_type";s:4:"free";s:6:"status";s:6:"Active";s:12:"payment_type";s:0:"";s:13:"autoresponder";s:10:"mgm_aweber";s:10:"subscribed";s:1:"Y";s:22:"autoresponder_notified";s:1:"Y";s:13:"user_password";s:16:"Lwp9E8RZECyB9bzb";s:17:"active_num_cycles";i:1;s:7:"pack_id";s:2:"22";s:12:"account_desc";s:12:"Free Account";s:16:"hide_old_content";s:1:"0";} [meta_value] => a:35:{s:2:"id";i:185;s:13:"custom_fields";a:6:{s:10:"first_name";s:5:"Kevin";s:9:"last_name";s:13:"RealLastNameHERE";s:5:"email";s:23:"RealuserNameHERE";s:8:"username";s:7:"RealuserNameHERE";s:8:"password";s:16:"Lwp9E8RZECyB9bzb";s:13:"password_conf";N;}s:22:"other_membership_types";a:0:{}s:12:"payment_info";a:0:{}s:6:"coupon";a:2:{s:12:"update_usage";b:0;s:15:"coupon_usage_id";b:0;}s:7:"upgrade";a:1:{s:6:"coupon";a:0:{}}s:6:"extend";a:0:{}s:4:"code";s:10:"mgm_member";s:4:"name";s:10:"Member Lib";s:11:"description";s:10:"Member Lib";s:7:"setting";a:0:{}s:6:"saving";b:1;s:8:"trial_on";i:0;s:10:"trial_cost";d:0;s:14:"trial_duration";i:0;s:19:"trial_duration_type";s:1:"d";s:16:"trial_num_cycles";i:0;s:8:"duration";i:1;s:13:"duration_type";s:1:"l";s:6:"amount";d:0;s:8:"currency";s:3:"USD";s:9:"join_date";i:1383183050;s:13:"last_pay_date";s:0:"";s:11:"expire_date";s:0:"";s:15:"membership_type";s:4:"free";s:6:"status";s:6:"Active";s:12:"payment_type";s:0:"";s:13:"autoresponder";s:10:"mgm_aweber";s:10:"subscribed";s:1:"Y";s:22:"autoresponder_notified";s:1:"Y";s:13:"user_password";s:16:"Lwp9E8RZECyB9bzb";s:17:"active_num_cycles";i:1;s:7:"pack_id";s:2:"22";s:12:"account_desc";s:12:"Free Account";s:16:"hide_old_content";s:1:"0";} ) [12] => Array ( [0] => 2579 [umeta_id] => 2579 [1] => 185 [user_id] => 185 [2] => _wpsc_customer_profile [meta_key] => _wpsc_customer_profile [3] => a:5:{s:16:"shipping_country";s:2:"US";s:15:"billing_country";s:2:"US";s:15:"shipping_region";s:2:"22";s:14:"billing_region";s:2:"22";s:4:"cart";s:1056:"O:9:"wpsc_cart":37:{s:16:"delivery_country";s:2:"US";s:16:"selected_country";s:2:"US";s:15:"delivery_region";s:2:"22";s:15:"selected_region";s:2:"22";s:24:"selected_shipping_method";N;s:24:"selected_shipping_option";N;s:24:"selected_shipping_amount";N;s:6:"coupon";N;s:14:"tax_percentage";N;s:9:"unique_id";s:40:"905310a45d51d75129b9de961ae17f69a7129ced";s:6:"errors";a:0:{}s:9:"total_tax";N;s:13:"base_shipping";N;s:19:"total_item_shipping";N;s:14:"total_shipping";N;s:8:"subtotal";N;s:11:"total_price";N;s:13:"uses_shipping";N;s:13:"is_incomplete";b:1;s:10:"cart_items";a:0:{}s:9:"cart_item";N;s:15:"cart_item_count";i:0;s:17:"current_cart_item";i:-1;s:11:"in_the_loop";b:0;s:16:"shipping_methods";b:0;s:15:"shipping_method";N;s:21:"shipping_method_count";i:1;s:23:"current_shipping_method";i:-1;s:18:"in_the_method_loop";b:0;s:15:"shipping_quotes";a:0:{}s:14:"shipping_quote";N;s:20:"shipping_quote_count";i:0;s:22:"current_shipping_quote";i:-1;s:17:"in_the_quote_loop";b:0;s:12:"coupons_name";s:0:"";s:14:"coupons_amount";i:0;s:15:"shipping_option";N;}";} [meta_value] => a:5:{s:16:"shipping_country";s:2:"US";s:15:"billing_country";s:2:"US";s:15:"shipping_region";s:2:"22";s:14:"billing_region";s:2:"22";s:4:"cart";s:1056:"O:9:"wpsc_cart":37:{s:16:"delivery_country";s:2:"US";s:16:"selected_country";s:2:"US";s:15:"delivery_region";s:2:"22";s:15:"selected_region";s:2:"22";s:24:"selected_shipping_method";N;s:24:"selected_shipping_option";N;s:24:"selected_shipping_amount";N;s:6:"coupon";N;s:14:"tax_percentage";N;s:9:"unique_id";s:40:"905310a45d51d75129b9de961ae17f69a7129ced";s:6:"errors";a:0:{}s:9:"total_tax";N;s:13:"base_shipping";N;s:19:"total_item_shipping";N;s:14:"total_shipping";N;s:8:"subtotal";N;s:11:"total_price";N;s:13:"uses_shipping";N;s:13:"is_incomplete";b:1;s:10:"cart_items";a:0:{}s:9:"cart_item";N;s:15:"cart_item_count";i:0;s:17:"current_cart_item";i:-1;s:11:"in_the_loop";b:0;s:16:"shipping_methods";b:0;s:15:"shipping_method";N;s:21:"shipping_method_count";i:1;s:23:"current_shipping_method";i:-1;s:18:"in_the_method_loop";b:0;s:15:"shipping_quotes";a:0:{}s:14:"shipping_quote";N;s:20:"shipping_quote_count";i:0;s:22:"current_shipping_quote";i:-1;s:17:"in_the_quote_loop";b:0;s:12:"coupons_name";s:0:"";s:14:"coupons_amount";i:0;s:15:"shipping_option";N;}";} ) [13] => Array ( [0] => 2580 [umeta_id] => 2580 [1] => 185 [user_id] => 185 [2] => purchased_instances [meta_key] => purchased_instances [3] => Cardio Stretch/Flex on 11/04/2013 8:00 AM [meta_value] => Cardio Stretch/Flex on 11/04/2013 8:00 AM )
==============================================================================
New working code:
echo '<table border=1><tr><td>Full Name</td><td>Class Purchased</td></tr>';
//Build Student Info1 table
foreach($userID as $val) {
$user_id= $val;
$queryA="SELECT meta_value FROM wp_usermeta WHERE meta_key ='first_name' AND user_id ='$user_id'";
$queryB="SELECT meta_value FROM wp_usermeta WHERE meta_key ='last_name' AND user_id ='$user_id'";
$queryC="SELECT meta_value FROM wp_usermeta WHERE meta_key ='purchased_instances' AND user_id ='$user_id'";
$resultA = $link->query($queryA)or die($link->error);
$resultB = $link->query($queryB)or die($link->error);
$resultC = $link->query($queryC)or die($link->error);
$numPurchases = $resultC->num_rows;
if($numPurchases > 0){
$i=0;
foreach($resultA as $val){
$fName[$i]=$val['meta_value'];;
$i++;
}
$i=0;
foreach($resultB as $val){
$lName[$i]=$val['meta_value'];
$i++;
}
$i=0;
foreach($resultC as $val){
$classDetails[$i]=$val['meta_value'];
$i++;
}
for($i=0; $i < $numPurchases; $i++){
echo '<td>';
print_r($fName[$i]);
echo ' ';
print_r($lName[$i]);
echo '</td><td>';
print_r($classDetails[$i]);
echo '</td></tr>';
}
/* free result set */
mysqli_free_result($resultA);
mysqli_free_result($resultB);
mysqli_free_result($resultC);
}
}
echo '';
Some important notes:
Validate any user input
NEVER execute SQL in a loop
Use one query that fetches everything you need (you're using the same table)
Fetch an associative array, this will help you a lot
Specify exactly what you need in your projection (not * after SELECT, exactly state each field to fetch like SELECT foo, bar, …)
Here is some code to help you get started with the above tips:
<?php
// Validate input, please note that prepared statements would be best.
// Also note that we don't need mysql escape calls because we validate
// that this is an actual integer value which can't contain anything
// dangerous. If we'd be working with any kind of string, date, ... the
// world would look different.
if (!is_array($userID)) {
if (!is_int($userID)) {
throw new \RuntimeException;
}
$userIds = "= {$userID}";
}
else {
$userIds = null;
$c = count($userID);
for ($i = 0; $i < $c; ++$i) {
if (!is_int($userID[$i])) {
throw new \RuntimeException;
}
$userIds .= $userIds ? "," : "IN(";
$userIds .= $userID[$i];
}
$userIds .= ")";
}
// One query to rule them all and nice formatting for readability.
$result = $link->query(
"SELECT *
FROM `wp_usermeta`
WHERE `meta_key` = 'purchased_instances'
AND `user_id` {$userIds}"
);
// Now we fetch everything within a single loop.
while ($row = mysqli_fetch_assoc($result)) {
print_r($row);
}
// That's it!
mysqli_free_result($result);
?>
I'm still trying to wrap my head around passing db query results from a model back to controller and finally to a view. I seem to be getting the data to the right place, I'm just not sure how to best access the resulting array of objects in the view.
Specifically, I'm trying to query my db for the most recent 7 distinct dates that someone has submitted a link. I get back an array of dates, and then for each of those dates I do a query for all links submitted on that date and store the results in an array. Then in the view, for each of those distinct dates, I show a header (the date), immediately followed by the links associated with it.
The array that come from my distinct date query ($link_headers):
Array (
[0] => stdClass Object([added_date] => 2011-08-11)
[1] => stdClass Object([added_date] => 2011-05-03)
[2] => stdClass Object([added_date] => 2011-04-21)
[3] => stdClass Object([added_date] => 2011-04-10)
[4] => stdClass Object([added_date] => 2011-03-04)
[5] => stdClass Object([added_date] => 2011-02-28)
[6] => stdClass Object([added_date] => 2011-02-22)
)
The array that comes from my query for actual links submitted ($links_result):
Array
(
[0] => Array
(
[0] => stdClass Object
(
[user_id] => 2
[link_id] => 1178
[link_url] => http://www.amazon.com/Silicone-Rubber-CASSETTE-Design-IPHONE/dp/B004YDJWOY
[link_name] => Silicone Skin BLACK CASSETTE TAPE
[link_notes] => iPhone case... probably won't fit in my dock.
[added_date] => 2011-08-11
[flag_new] => 1
[rating] => 4
[public] => 1
)
)
[1] => Array
(
[0] => stdClass Object
(
[user_id] => 2
[link_id] => 1177
[link_url] => http://snorby.org/
[link_name] => Snorby - Snort front-end
[link_notes] =>
[added_date] => 2011-05-03
[flag_new] => 1
[rating] => 4
[public] => 1
)
)
[2] => Array
(
[0] => stdClass Object
(
[user_id] => 2
[link_id] => 1176
[link_url] => http://www.nytimes.com/2011/04/17/business/17excerpt.html?_r=4&pagewanted=1&ref=business
[link_name] => Corner Office - The 5 Habits of Highly Effective C.E.O.s
[link_notes] => Sounds a lot like what Nathanial said...
[added_date] => 2011-04-21
[flag_new] => 1
[rating] => 4
[public] => 1
)
)
[3] => Array
(
[0] => stdClass Object
(
[user_id] => 2
[link_id] => 1175
[link_url] => http://chezlarsson.com/myblog/2010/06/panduro-concrete-challenge-3.html
[link_name] => Concrete book-ends
[link_notes] => Cool look...
[added_date] => 2011-04-10
[flag_new] => 1
[rating] => 4
[public] => 1
)
[1] => stdClass Object
(
[user_id] => 2
[link_id] => 1174
[link_url] => http://themeforest.net/item/reciprocity-photo-blog-gallery/154590
[link_name] => Site Templates - Reciprocity - Photo Blog
[link_notes] =>
[added_date] => 2011-04-10
[flag_new] => 1
[rating] => 5
[public] => 1
)
)
[4] => Array
(
[0] => stdClass Object
(
[user_id] => 2
[link_id] => 1173
[link_url] => http://lifehacker.com/#!5771943/the-always-up+to+date-guide-to-jailbreaking-your-ios-device
[link_name] => The Always Up-to-Date Guide to Jailbreaking Your iOS Device
[link_notes] =>
[added_date] => 2011-03-04
[flag_new] => 1
[rating] => 4
[public] => 1
)
)
[5] => Array
(
[0] => stdClass Object
(
[user_id] => 2
[link_id] => 1172
[link_url] => http://lifehacker.com/#!5754463/how-to-jailbreak-your-ios-421-device
[link_name] => How to Jailbreak Your iOS 4.2.1 Device
[link_notes] =>
[added_date] => 2011-02-28
[flag_new] => 1
[rating] => 4
[public] => 1
)
)
[6] => Array
(
[0] => stdClass Object
(
[user_id] => 2
[link_id] => 1171
[link_url] => http://www.bitplumber.net/2010/10/a-cassandra-hardware-stack-dell-c1100s-ocz-vertex-2-ssds-with-sandforce-arista-7048s/
[link_name] => A Cassandra Hardware Stack
[link_notes] =>
[added_date] => 2011-02-22
[flag_new] => 1
[rating] => 3
[public] => 1
)
)
)
... all seems fine enough. But my problem comes from my view, where I'm trying to build the HTML as described above. A simplified view of the code I'm trying to get to work is as follows:
foreach ($link_headers as $header) {
echo "INDEX: ". $links_headers .", ADDED DATE: ". $header->added_date ."<BR>";
foreach ($links_result[$link_headers] as $result){
echo $result->added_date ."<BR>";
echo $result->link_name ."<BR><BR>";
}
}
So, I'm trying to use the index of the first one to tell my foreach loop which index of the second array to loop through and get the content. Clearly I'm misusing the $links_result[$link_headers] variable(s) but I left it in to show what I was trying to do.
Any help is very much appreciated!
Michael
I dont use CodeIgniter but whether within th framework or from PHP i would just grab it all in one go and then the issue with the indexes becomes moot:
SELECT * FROM model_table mt WHERE mt.added_date IN (
SELECT DISTINCT md.added_date from model_table md
ORDER BY md.added_date DESC
LIMIT 7
) ORDER BY mt.added_date DESC
That should get you an array of models ordered by date limted to the 7 most recent dates. So then its just a matter of choosing when to display a header:
$current = null;
foreach($links as $link) {
if($link->added_date !== $current) {
// show the header and set current to the current date
$current = $link->added_date;
echo 'HEADER: Added on ' . $current . '<br />';
}
echo $row->added_date ."<BR>";
echo $row->link_name ."<BR><BR>";
}