Sorry to have to ask this, but I have searched and searched and can't seem to figure out how to do this...
I have this code on a custom PHP page:
(code placeholder spot which I will reference later)
$races = $wpdb->get_results("
select r.race_name
,r.race_id
,date_format(r.race_date,'%c.%d.%Y') race_date
from race_calendar r
order by r.race_date;
");
foreach ( $races as $race ) {
echo $race->race_id . ',' . $race->race_name . ',' . $race->race_date;
}
Which displays something like this:
1, Resolution, 2017-01-17
2, Sea 2 Sea, 2017-03-02
3, Earth Day, 2017-04-22
But, here's my question...
I need to be able to add a custom row at the beginning of the array.
So, I need to be able to add some code at the exact place above where I have (code placeholder spot which I will reference later) which will insert anything I want as the first row of the array.
I want to be able add a custom row, for example, "id, name, date" so that when I run the display code it will display this:
id, name, date
1, Resolution, 2017-01-17
2, Sea 2 Sea, 2017-03-02
3, Earth Day, 2017-04-22
I've tried a few things I've found online but nothing seems to work.
And no, a simple 'echo' command won't do. I don't want to go into details why (because it will make this post very long), but in my case I need the custom row added to the array.
Please help!
=================== EDIT (ANSWERED!!) ======================
Thanks to @Abdul I got it to work with this code:
$races_to_send = array(array ('name', 'id', 'date'));
$races = $wpdb->get_results("
select r.race_name
,r.race_id
,date_format(r.race_date,'%c.%d.%Y') race_date
from race_calendar r
order by r.race_name
,r.race_date;
", ARRAY_N);
$merged_arr = array_merge($races_to_send, $races);
foreach ( $merged_arr as $race ) {
echo $race['0'] . ',' . $race['1'] . ',' . $race['2'];
}
echo()
function call before theforeach
loop, i.e.echo("here are some values")
. Please review the "How to Ask" page in our help center.echo "here are some values";
? This looks like a basic PHP questionecho()
for some reason, then just close out the PHP tag before the content, write it verbatim, then open the PHP tag once more.?> here are some values <br> <?php foreach( //...
id, name, date
. How then could I add the row to the array?