简体   繁体   中英

Accessing array data from mysql database and php

I have two mysql tables with information:

One table stores page information like its name and location as well as an array column to store what widgets to show per page stored in array format: 1,2,3,4

The other table is the actual widget table with the widget info and the id as its identifier ex: 1,2,3,4

I need to:

  1. Get array data from the page table
  2. Loop through that array and display content from the widget table.

Ive been working with foreach statements:

The url for each page follows this format index.php?page=thispage&route=thisroute thus the $_GET['page']

$page = $_GET['page'];
$order = "SELECT * FROM pages where name='$page'";
  $result = mysql_query($order);
    while($row = mysql_fetch_array($result)){
            $widget = array();
            $widget .= $row[3];
        }

        print_r($widget);

        foreach($widget as $w) {
                    $order = "SELECT * FROM menu where id='$w'";
                    $result = mysql_query($order);
                    while($row = mysql_fetch_array($result)){
                    print $row[1];
                    }
            }

UPDATE: Figured out a solution that works for me.

The reason behind keeping the comma separated values is because i could have one widget per page or 15 widgets per page and each widget can be added on the fly. And from the administration side i want to be able to select which widgets each page can have.

When keeping the data in comma separated values where all the data is in ONE column "1,2,3,4".

Ill just stick to exploding the data into an array and looping through each value of the array:

$page = $_GET['page'];
$order = "SELECT * FROM pages where name='$page'"; //select widgets to show on page
  $result = mysql_query($order);
    while($row = mysql_fetch_array($result)){
            $widget = $row[3]; 
            $string = preg_replace('/\,$/', '', $widget); //remove trailing comma
            $array = explode(', ', $string);

            foreach($array as $value) //loop over values
                {
                    $order = "SELECT * FROM widget where id='$value'";
                    $result = mysql_query($order);
                    while($row = mysql_fetch_array($result)){
                        print $row[1]; //print data: this will be formated to the specific divs set up.
                }

            }
    }
  1. Get array data from the page table: SELECT array_column_name FROM table_store_page WHERE id=your_id (use this sql to fetch the array in)

  2. Execute the SQL statement in php maybe using mysql_query

Anyway, STORING array/comma-separated values in Database is NEVER RECOMMENDED. You can try normalizing your Database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM