2

I have a mysql table which contains is:

folder_id (int(11), auto increment) 
folder_name (varchar) 
folder_class(varchar) 
folder_link (varchar)

What I want to do is somehow loop through the table and store each row like this:

$packs = array( array(  'Name'  => 'DarkUIPack', 
                    'Class' => 'ui',
                    'Link'  => 'http://graphicriver.net/item/dark-minimalist-ui-pack/662762' 
                ),
            array(  'Name'  => 'MinimalistIcons', 
                    'Class' => 'min',
                    'Link'  => 'http://graphicriver.net/item/small-minimalist-icons-pack/670469'
                ),
            array(  'Name'  => 'BlueMediaIcons', 
                    'Class' => 'blue',
                    'Link'  => 'http://graphicriver.net/item/blue-media-icons-set/705319'
                ),
            array(  'Name'  => 'MediaIcons', 
                    'Class' => 'med',
                    'Link'  => 'http://graphicriver.net/item/media-icons-set/679835'
                ),
            array(  'Name'  => 'ToTheTopButtons', 
                    'Class' => 'top',
                    'Link'  => 'http://graphicriver.net/item/to-the-top-buttons/673221'
                ),
            array(  'Name'  => 'Sunglasses', 
                    'Class' => 'sun',
                    'Link'  => ''
                ),
            array(  'Name'  => 'RealEstate', 
                    'Class' => 'est',
                    'Link'  => 'http://graphicriver.net/item/simple-real-estate-logo/724697'
                ),
            array(  'Name'  => 'PhotoStudio', 
                    'Class' => 'std',
                    'Link'  => 'http://graphicriver.net/item/photo-studio-logo/724694'
                ),
            array(  'Name'  => 'PayDayCity', 
                    'Class' => 'std',
                    'Link'  => ''
                ),
            array(  'Name'  => 'MoleculeCorp', 
                    'Class' => 'mol',
                    'Link'  => 'http://graphicriver.net/item/molecule-corp-logo/719307'
                ),
            array(  'Name'  => 'ClubbGX', 
                    'Class' => 'gx',
                    'Link'  => ''
                ),
            array(  'Name'  => 'AerialVision', 
                    'Class' => 'aer',
                    'Link'  => ''
                ),
            array(  'Name'  => 'ServiceCompany', 
                    'Class' => 'ser',
                    'Link'  => 'http://graphicriver.net/item/service-company-logo/727091'
                ),
            array(  'Name'  => 'ElectroTech', 
                    'Class' => 'ele',
                    'Link'  => 'http://graphicriver.net/item/electro-tech-logo/720904'
                ),
            array(  'Name'  => 'CreativeStudio', 
                    'Class' => 'cre',
                    'Link'  => 'http://graphicriver.net/item/creative-studio-logo/719494'
                ),
            array(  'Name'  => 'NanoCorp', 
                    'Class' => 'nan',
                    'Link'  => 'http://graphicriver.net/item/nano-corp-logo/719098'
                ),
            array(  'Name'  => 'RehabPlace', 
                    'Class' => 'reh',
                    'Link'  => ''
                ),
            array(  'Name'  => 'MyLocalMix', 
                    'Class' => 'mix',
                    'Link'  => ''
                ),
            array(  'Name'  => 'SevenBySeven', 
                    'Class' => 'sev',
                    'Link'  => ''
                ),
            array(  'Name'  => 'ComingSoon', 
                    'Class' => 'com',
                    'Link'  => ''
                ),
            array(  'Name'  => 'AlienIcons', 
                    'Class' => 'aln',
                    'Link'  => 'http://graphicriver.net/item/alien-icons-set/698515'
                ),
            array(  'Name'  => 'PreloaderPortfolio', 
                    'Class' => 'pre',
                    'Link'  => ''
                ),
            array(  'Name'  => 'BioTech', 
                    'Class' => 'bio',
                    'Link'  => ''
                ),
            array(  'Name'  => 'ConstructionCompany', 
                    'Class' => 'con',
                    'Link'  => ''
                ),
            array(  'Name'  => 'EagleMedia', 
                    'Class' => 'egl',
                    'Link'  => ''
                ),
            array(  'Name'  => 'ElectronicWays', 
                    'Class' => 'elw',
                    'Link'  => ''
                ),
            array(  'Name'  => 'EnvironmentalCompany', 
                    'Class' => 'env',
                    'Link'  => ''
                ),
            array(  'Name'  => 'SecureData', 
                    'Class' => 'sec',
                    'Link'  => 'http://graphicriver.net/item/secure-data-company-logo/907334'
                ),
            array(  'Name'  => 'ConstructSimple', 
                    'Class' => 'cns',
                    'Link'  => 'http://graphicriver.net/item/simple-construction-company-logo/907538'
                ),
            array(  'Name'  => 'ConstructRoof', 
                    'Class' => 'cnr',
                    'Link'  => 'http://graphicriver.net/item/construction-company-logo/907549'
                )
        );  

where 'Name' corresponds to folder_name, 'Class' to folder_class and 'Link' to folder_link.

I'm using this within a class and the class looks like this till now:

class folder
{
private $connect;

public function __construct() {

    $this->connect = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);

    if (mysqli_connect_errno()) {
        $error = true;
        $message['error'] = true;
        $message['message'] = mysqli_connect_error();
        return json_encode($message);
    }
        else {
            return true;    
        }

}

    public function crtFolder($fldName,$fldClass,$fldLink,$fldPath) {

    $fldName = preg_replace('/\s+/', '', $fldName);

    $fldPN = $fldPath."\\".$fldName;

    $modArray = array(array('1'));

    if ((!is_dir($fldPN))) {
        if(mkdir($fldPN,0777,true)) {

            $sql = "INSERT INTO folders (folder_name,folder_class,folder_link) VALUES (?, ?, ?)";

            if($stmt = $this->connect->prepare($sql)) {
                $stmt->bind_param("sss", $fldName, $fldClass, $fldLink);
                $stmt->execute();
                $stmt->close();
                $error = false;
                $message['error'] = false;
                $message['message'] = "Folder Created | Data Successfuly Inserted";
                return json_encode($message);
            }
                else {
                    $error = true;
                    $message['error'] = true;
                    $message['message'] = "Folder Created | Data Failed To Insert";
                    return json_encode($message);
                }
        }
            else { 
                $error = true;
                $message['error'] = true;
                $message['message'] = "Folder Failed To Create";
                return json_encode($message); 
            }
    }
        else {
            $error = true;
            $message['error'] = true;
            $message['message'] = "Folder Already Exists";
            return json_encode($message);   
        }
}

public function __destruct() {

    $closeConnection = $this->connect->close();

    if($closeConnection) {
        return true;
    }
}

}

I'm showing the class because I want to keep the same method for the creating the array as the other methods are created. Because I found something on Google, but it was making an array from multiple tables. I only have one table.

4
  • Sorry you want to arse the MySQL response to create the multiple Array? Commented Dec 14, 2011 at 10:52
  • So I found a way to do it, I'm not sure if it is correct... here's the code $sql = "SELECT folder_name, folder_class, folder_link FROM folders"; if($stmt = $this->connect->prepare($sql)) { $stmt->bind_result($fldName, $fldClass, $fldLink); $stmt->execute(); $final = array(); while ($row = $stmt->fetch()){ $stmt->bind_result($fldName, $fldClass, $fldLink); $final[] = array( 'Name' => $fldName, 'Class' => $fldClass, 'Link' => $fldLink ); } $stmt->close(); print_r($final); } Commented Dec 14, 2011 at 12:13
  • let me know if I'm doing it wright :D Commented Dec 14, 2011 at 12:13
  • does it work? it looks fine from where I stand. Commented Dec 14, 2011 at 12:17

3 Answers 3

3

Write below given function in to your class. Hope this will help things.

public function getTableData(){

 $sql = "SELECT * FROM folders";
 if($stmt = $this->connect->prepare($sql)) {

     $stmt->execute();
     $stmt->close();
     $arrFinalArray = array();
     while ($arrFolder = $stmt->fetch()){

        $arrFinalArray[] = array(
                    'Name'  => $arrFolder['folder_name'], 
                    'Class' => $arrFolder['folder_class'],
                    'Link'  => $arrFolder['folder_link']
        );


     }
  }
}
Sign up to request clarification or add additional context in comments.

9 Comments

shouldn't foreach have 'as' in there,because if I paste it in dreamweaver it tells me that there is a syntax error ?
ohh that my mistake...I have updated my post..then after forget to change loop to while......please see my post again
$value is undefined. you mean $arrFolder there.
now I get this error : Warning: mysqli_stmt::fetch() expects exactly 0 parameters, 1 given in C:\xampp\htdocs\xworkflow\test.php on line 99 on this line while ($arrFolder = $stmt->fetch($arrResult)){...
I have changed the code little bit....remove parameter from fetch(). Please try now
|
1

Following your format:

if($stmt = $this->connect->prepare($sql)) {
    $stmt->bind_param("sss", $fldName, $fldClass, $fldLink);
    $stmt->execute();

    $result = array();

    while ($stmt->fetch()) {
        $result[] = array('Name' => $fldName, 'Class' => $fldClass, 'Link' => $fldLink);
    }

    print_r($result);
}

It should work.

3 Comments

this doesn't work, I tried $sql = 'SELECT * FROM folders' but it tells me that I don't have the number of fields in prepared statement...
Try SELECT folder_name, folder_class, folder_link FROM folders
the same thing, if I do it like that it will tell me the same thing and if I add WHERE folder_name = ? .. nothing happens ...
1

Try something like this :

$result = SOMETHING_FROM_QUERY;
$packs  = new array();
while ($row = mysql_fetch_assoc($result)) {
    array_push($packs,array('Name'  => $row['folder_name'], 'Class' => $row['folder_class'], 'Link'  => $row['folder_link']));
}

3 Comments

No, the database is already populated with about 30 rows. Now I want to take all of that data from the table and put it in an array like I have $packs.
That should do the try. $pack is an array of associative arrays that I copy pasted from your code. So if your code respect your spec, then this cod should as well.
Edit the code to get the values from the $row not hard coded anymore

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.