Forum Moderators: coopster

Message Too Old, No Replies

php array to hold database rows

         

Dunjohn19

3:16 pm on Aug 19, 2020 (gmt 0)

5+ Year Member



Hello everyone,

i am building a bookmarking system for my website, so that users can bookmark pages.
i have all of my php code setup for this sytem, now i need to add the database code.

i have played with code and it is working but i can't figure out a way to organize the data coming out of the database.

$results holds an array of all of the db data. I want this data to be placed into arrays corresponding to icon types.

i have two icon types: circles (border-radius: 50%;) and squares/rectangles.
*i know the diff between a sq and a rectangle but the word squares is a smaller variable name than rectangles, so i call them squares.

i have tried several ways to separate the types into arrays but i am unable to load each row into its own array.

bookmarks columns in database: type, name, icon, link.
i'd like to have a new array named $circles which should hold the row data:
$circles = ['','','',''];

i guess that my real problem is not understanding how to build this array.

while ($results = $sth->fetch()) {
switch ($results['type']) {
case 'circle':
$circles[$results['icon']] = $results['link'];
break;
case 'square':
$squares[$results['icon']] = $results['link'];
break;
}
}

i just added $circles[$results['icon']] = $results['link']; today because i don't know how to construct this array.
the entire row should be in $circles. so i guess $circles[0] should hole an array which holds the row values?

what i am trying to do is display the bookmarks by type on the page.
circles require different css than squares (maybe better described as rectangles).
so on my html page, i want all circle icon bookmarks displayed in one div
and all square/rectangle icon bookmarks displayed in a separate div.

i all ready have the bookmark data set up.
i just need the database created, then i need to extract this data.
i've practiced and it is working but i need the data to be categorized when it comes out of the database.

i am a novice with this code. I don't know how to loop through the code to separate it.

anyone able to offer tips of how to accomplish this task?

Best wishes,
John

[edited by: phranque at 7:30 am (utc) on Aug 20, 2020]
[edit reason] disable graphic smile faces [/edit]

nordberg25

7:44 pm on Aug 19, 2020 (gmt 0)

5+ Year Member



Off the top of my head it seems like this is what you want to do, since there is only 4 elements to the array I just added the array keys manually

bookmarks columns in database: type, name, icon, link.
i'd like to have a new array named $circles which should hold the row data:
$circles = ['','','',''];


while ($results = $sth->fetch()) {
switch ($results['type']) {
case 'circle':
$circles[0] = $results['type'];
$circles[1] = $results['name'];
$circles[2] = $results['icon'];
$circles[3] = $results['link'];
break;
case 'square':
$squares[0] = $results['type'];
$squares[1] = $results['name'];
$squares[2] = $results['icon'];
$squares[3] = $results['link'];
break;
}
}

Dunjohn19

4:50 am on Aug 20, 2020 (gmt 0)

5+ Year Member



Hi Nordberg,

Thank you for replying. I was tinkering with the code yesterday and i remembered array_push, so i tried it.

 while ($results = $sth->fetch()) {
switch ($results['type']) {
case 'circle': array_push($circles, $results); break;
case 'square': array_push($squares, $results); break;
}
}


I will also try your code to see if it helps me accomplish my tasks. I need to design the test html now using these new arrays ($circles and $squares).

Best wishes,
John