0

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'];
    }
7
  • 1
    Hello @mannyotr - Welcome to the WordPress Development community! As your question stands it may be considered "off-topic" - while it occurs in the context of WordPress, it's not actually a question about WordPress or WordPress development. Rather, your question shows a lack of basic PHP knowledge (and thus research effort). Achieving your desired outcome does not require altering the database or inserting rows into results - just place your text in an echo() function call before the foreach loop, i.e. echo("here are some values"). Please review the "How to Ask" page in our help center.
    – bosco
    Commented Mar 21, 2017 at 23:14
  • echo "here are some values";? This looks like a basic PHP question
    – Tom J Nowell
    Commented Mar 21, 2017 at 23:30
  • I don't want to go into the detail of why, but I can't use an echo command. The custom row has to be inserted into the array. If you really need the details I will provide them, but an echo command will not work in my case.
    – mannyotr
    Commented Mar 21, 2017 at 23:35
  • The text you are trying to insert does fit the format of one of your "races" - thus it is illogical to insert it into an array of races, and attempt to print it to the screen as though it were a race. Breaking separation of concerns in such a manner would make your code needlessly complex. If you can't use an echo() 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( //...
    – bosco
    Commented Mar 21, 2017 at 23:50
  • @bosco, I have modified my question. Assume the row I want to insert does fit the format. For example, the new row would be id, name, date. How then could I add the row to the array?
    – mannyotr
    Commented Mar 21, 2017 at 23:55

2 Answers 2

0

What about something like this ?

$custom_row = array(array('race_id' => '1', 'race_name' => '2', 'race_date' => '3'));

$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;
      ", ARRAY_N);
$merged_arr = array_merge($custom_row, $races);
foreach ( $merged_arr as $race ) {
  echo $race['race_id'] . ',' . $race['race_name'] . ',' . $race['race_date'];
}

UPDATE

$custom_row = array(array('1', '2', '3'));

$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;
      ", ARRAY_N);
$merged_arr = array_merge($custom_row, $races);
foreach ( $merged_arr as $race ) {
  echo $race['0'] . ',' . $race['1'] . ',' . $race['2'];
}
9
  • I had already tried that. When I did the first row displayed simply contained () and nothing else, followed by the rest of the rows as expected.
    – mannyotr
    Commented Mar 21, 2017 at 23:41
  • Looks like you've used a modified version of this code. Try using exact same code. specially ARRAY_N is important here Commented Mar 22, 2017 at 0:11
  • I have modified my original post to explain why that didn't work.
    – mannyotr
    Commented Mar 22, 2017 at 0:31
  • Have you used the same code I've provided ? Commented Mar 22, 2017 at 0:41
  • Yes, the exact code you provided is what I used on my EDIT 03.
    – mannyotr
    Commented Mar 22, 2017 at 0:51
0

If you haven't solved the problem you should try to use numeric array in case of your race_to_add. It should help to return correct array for printing function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.