Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Trying to get array keys

  • This topic is empty.
Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #46217
    chrisburton
    Participant

    ###Code to get data:

    $query = “SELECT * FROM `read`”;
    $result = $DB->query($query);
    while ($row = $result->fetch_assoc()) {
    var_dump($row);
    }

    ###Output:

    array (size=3)
    ‘order’ => string ‘1’ (length=1)
    ‘title’ => string ‘Google Maps (2013 Update)’ (length=25)
    ‘url’ => string ‘http://fontsinuse.com/uses/4135/google-maps-2013-update’ (length=55)
    array (size=3)
    ‘order’ => string ‘2’ (length=1)
    ‘title’ => string ‘Type Battle 41! Five characters, no curves.’ (length=43)
    ‘url’ => string ‘http://typophile.com/battle41’ (length=29)
    array (size=3)
    ‘order’ => string ‘3’ (length=1)
    ‘title’ => string ‘Desk = Work; Not Desk = Not Work’ (length=32)
    ‘url’ => string ‘http://chriscoyier.net/2013/05/21/desk-work-not-desk-not-work/’ (length=62)

    ###Desired Output

    array (size=3)
    => 0
    ‘order’ => string ‘1’ (length=1)
    ‘title’ => string ‘Google Maps (2013 Update)’ (length=25)
    ‘url’ => string ‘http://fontsinuse.com/uses/4135/google-maps-2013-update’ (length=55)
    array (size=3)
    => 1
    ‘order’ => string ‘2’ (length=1)
    ‘title’ => string ‘Type Battle 41! Five characters, no curves.’ (length=43)
    ‘url’ => string ‘http://typophile.com/battle41’ (length=29)
    array (size=3)
    => 2
    ‘order’ => string ‘3’ (length=1)
    ‘title’ => string ‘Desk = Work; Not Desk = Not Work’ (length=32)
    ‘url’ => string ‘http://chriscoyier.net/2013/05/21/desk-work-not-desk-not-work/’ (length=62)

    How can I do this?

    #141949
    TheDoc
    Member

    If each array is $row, then can’t you just do whatever you need to do to each of them in your while loop?

    If you do need it as a separate array, I think you can do something like this:

    $newArray = array();

    while ( $row = $result->fetch_assoc() ) {
    array_push($newArray, $row);
    }

    But probably come up with a better name than `newArray` ;)

    Note: not PHP expert.

    #141950
    chrisburton
    Participant

    @TheDoc That’s exactly what I’m trying to figure out.

    What’s happening with your code is that it outputs a new array in a continuous loop.

    ###Result

    array (size=1)
    0 =>
    array (size=3)
    ‘order’ => string ‘1’ (length=1)
    ‘title’ => string ‘Google Maps (2013 Update)’ (length=25)
    ‘url’ => string ‘http://fontsinuse.com/uses/4135/google-maps-2013-update’ (length=55)
    array (size=2)
    0 =>
    array (size=3)
    ‘order’ => string ‘1’ (length=1)
    ‘title’ => string ‘Google Maps (2013 Update)’ (length=25)
    ‘url’ => string ‘http://fontsinuse.com/uses/4135/google-maps-2013-update’ (length=55)
    1 =>
    array (size=3)
    ‘order’ => string ‘2’ (length=1)
    ‘title’ => string ‘Type Battle 41! Five characters, no curves.’ (length=43)
    ‘url’ => string ‘http://typophile.com/battle41’ (length=29)
    array (size=3)
    0 =>
    array (size=3)
    ‘order’ => string ‘1’ (length=1)
    ‘title’ => string ‘Google Maps (2013 Update)’ (length=25)
    ‘url’ => string ‘http://fontsinuse.com/uses/4135/google-maps-2013-update’ (length=55)
    1 =>
    array (size=3)
    ‘order’ => string ‘2’ (length=1)
    ‘title’ => string ‘Type Battle 41! Five characters, no curves.’ (length=43)
    ‘url’ => string ‘http://typophile.com/battle41’ (length=29)
    2 =>
    array (size=3)
    ‘order’ => string ‘3’ (length=1)
    ‘title’ => string ‘Desk = Work; Not Desk = Not Work’ (length=32)
    ‘url’ => string ‘http://chriscoyier.net/2013/05/21/desk-work-not-desk-not-work/’ (length=62)

    #141951
    TheDoc
    Member

    That’s only because you have the var_dump inside of the `while`. Put `var_dump($newArray);` after the loop.

    #141952
    chrisburton
    Participant

    @TheDoc You’re so smart. Thanks a lot for the quick help.

    #141953
    TheDoc
    Member

    Yahoo, I answered one of your PHP questions!

    #141955
    Alen
    Participant

    Are you still trying to figure out the random order in which items are inserted into the database?

    Why don’t you convert `pubDate` into UTC timestamp by:

    $pubDate = $items;
    $utcTime – date(strtotime($pubDate));

    Then store that into the database via Integer data type, and make it unique. This way you can sort by timestamp.

    Tested and working: https://gist.github.com/alenabdula/5913146

    #141956
    Alen
    Participant

    Wait… that is article publish date not readability.com xml add date? Right?

    Nope, I just checked timestamp “Leaked report…” title shows Monday 8th July 2013 3:12:13 PM UTC. You might need to pass time zone for date function above to get the correct time zone.

    #141957
    chrisburton
    Participant

    @AlenAbdula Correct. So unfortunately that wouldn’t work. However, @traq helped me out with that in my previous thread which is thankfully solved now.

    #141958
    Alen
    Participant

    Nevermind, I see you’re trying to do something else. I misinterpreted the question. Carry on… :)

    #141959
    chrisburton
    Participant

    @AlenAbdula thanks for trying to help out.

Viewing 11 posts - 1 through 11 (of 11 total)
  • The forum ‘Back End’ is closed to new topics and replies.