Jump to content

[SOLVED] A question about functions, arrays, and objects!


Tuskony

Recommended Posts

Hey guys I got another question here.

 

On my site someone can "Create a Truck" through a form which basically takes the forms info and slaps it into a database. When the user visits a certain page I want to pull that info from the database and create a truck object for each truck found.

 

I basically have it all working but now I'm stuck on one little part.... well a major part.

 

This is basically my sequence of code:

 

1. User creates a truck.

2. Site stores truck in database.

3. User visits a page where the trucks are pulled from the database by LoadTrucks().

 

LoadTrucks() is in an include file and gets called from trucks.php.

 

I want LoadTrucks() to return an array of Truck object types. How do I do this?

 

Here is what I have so far!

 

This is on the calling page

    $id = $_SESSION['id'];

    //Load this users trucks!
    $Trucks = LoadTrucks($id);

 

This is the LoadTrucks function

   function LoadTrucks($id) {

        $Trucks = array();
        
	mysql_connect('localhost', 'MyTMaxx', 'doherty19');
	mysql_select_db('mytmaxx');

	$query = "select * from trucks where id = " . $id;

	$result = mysql_query($query);

	if(!$result) {
		return 0;
	}

	//We found no results
	if(!mysql_num_rows($result)) {
		return 0;
	}

	$i = 0;

	while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {

		$Trucks[] = new Truck();

		$Trucks[$i]->SetName($row['Name']);

		$i += 1;
	}

        return $Trucks;
    }

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.