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;
    }

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.