PDA

View Full Version : Classes video tutorial-format results outside class


jaymo
03-11-2009, 08:35 PM
Hi all,

Having a bit of trouble with this class. I am using the one from Davids classes video tutorial. I changed it a bit tho cos I want to format the data outside the class rather than in it. Class:
class ToolBox{

// Variables
var $connect;
var $dbase;

// Init Consturctor
function ToolBox(){}

// Connect to database
function Connector($hostname, $database, $username, $password){

$this->connect = mysql_connect($hostname, $username, $password) or trigger_error(mysql_error(), E_USER_ERROR);
$this->dbase = mysql_select_db($database, $this->connect);
}

// Query database
function ask_the_db($SQL){

$query = sprintf($SQL);
$send = @mysql_query($query);
return $result = @mysql_fetch_array($send);

}
}Now in my main page I call the connect and run a query:
$result = $instance->ask_the_db(" SELECT customer_id, name, etc..)To format the output in a table I go:
do { ?>
<tr>
<td>&nbsp;</td>
<td><?php echo $result['customer_id']; ?> </td>
<td><?php echo $result['order_id']; ?></td>
<td><?php echo $result['name'];/* */ ?></td>
<td><?php echo $result['address']; ?></td>
</tr>
<?php } while($unknown=mysql_fetch_array($result) ) ?>
</table>The while statement is the problem and this only outputs the first line of the database. I used to use a variable that pointed to the @mysql_query instead of unknown. In the class case it is called $send but it is in the class. Do i need to return it? Hope you can help, im sure this is something easy ive missed :(

davidj
03-12-2009, 10:33 AM
i advise you do the loop / table creation using a method

what i do is return my result into a multi - dimensional array so the first key contains the first row data.

once you have the result in this format then its easy to write a method which can manipulate this array

also i would not use underscores to define methods