Autor | Zpráva | ||
---|---|---|---|
chvostik Profil |
#1 · Zasláno: 14. 11. 2014, 13:04:35
Zdravim, mám problem v OOP, že nevím jak dostat pomocí funkce z databáze víc jak jeden řádek. Nějaké rady? Zkoušim to na nasledujícím kodu:
<?php session_start(); define( "FRAMEWORK_PATH", dirname( __FILE__ ) ."/" ); require_once('registry/registry.class.php'); $registry = PHPEcommerceFrameworkRegistry::singleton(); require_once('config.php'); $registry->storeObject('mysql.database','db'); $registry->getObject('db')->newConnection($config['db_ecom_host'],$config['db_ecom_user'],$config['db_ecom_pass'],$config['db_ecom_name']); //nacteni content z db $data=$registry->getObject('db')->readRecords('content'); ?> <!DOCTYPE html> <html> <head> <title></title> </head> <body> <?php foreach( $data as $content ){ echo "<span> $content </span> <a href = \"index.php?content_order=$content\" > Pridat do kosiku </a> <br>"; } ?> </body> </html> <?php class mysqldatabase { public function newConnection( $host, $user, $password, $database ) { $this->connections[] = new mysqli( $host, $user, $password, $database ); $connection_id = count( $this->connections )-1; if( mysqli_connect_errno() ) { trigger_error('Chyb při připojování k hostiteli. '.$this->connections[$connection_id]->error, E_USER_ERROR); } return $connection_id; } public function readRecords( $table) { $read = "SELECT name from $table"; $this->executeQuery( $read ); return $this->getRows(); } public function executeQuery( $queryStr ) { if( !$result = $this->connections[$this->activeConnection]->query( $queryStr ) ) { echo $queryStr; trigger_error('Chyba při provádění dotazu: '.$this->connections[$this->activeConnection]->error, E_USER_ERROR); } else { $this->last = $result; } } public function lastInsertID() { return $this->connections[ $this->activeConnection]->insert_id; } public function getRows() { return $this->last->fetch_array(MYSQLI_ASSOC); } } ?> |
||
Tori Profil |
To je z té knížky od Peacocka, co? Metoda fetch_array (ř. 46) je normální metoda třídy
mysqli_result , vrací buď jednotlivé řádky dat (při prvním volání první řádek, při druhém druhý atd.), anebo FALSE, pokud ve výsledku už nejsou žádné záznamy. Peacock tu metodu pojmenoval špatně, mělo by to spíš být getRow() .
PS: Nevím, u které jste kapitoly; až narazíte na chybu „Strict Standards: Non-static method PHPEcommerceFrameworkRegistry::getSetting() should not be called statically...“, pokračujte vláknem Non-static method. |
||
chvostik Profil |
#3 · Zasláno: 14. 11. 2014, 16:33:15
jj je to podle té knížky :-). Už jsem to opravil ..díky za pomoc :-)
//nacteni content z db $contents = array(); $i = 0; $registry->getObject('db')->readRecords('content'); while( $data = $registry->getObject('db')->getRows() ) { $contents[$i] = $data["name"]; $i++; } foreach( $contents as $content ){ echo "<span> $content </span> <a href = \"index.php?content_order=$content\" > Pridat do kosiku </a> <br>"; } |
||
Časová prodleva: 9 let
|
0