Welcome Guest, Not a member yet? Register   Sign In
Loop Within A Loop
#1

[eluser]Phil Norton[/eluser]
Hi

Hopefully someone can help. I'm storing a range of product_ids in serialized format in a table in the db for a very basic "product selector". Now, what I want to do, is to use this serialized array, and go off to the products table in the db, to get the relevant product info in order to generate links etc. At the minute, I'm doing 1 query in the model to get the basic data:

Code:
$text = $this->sitemodel->productSelector($answer);

and I can then send that array to the view (from the controller) as follows:

Code:
$data['products'] = unserialize($text->s_products);

which be an array of product_ids. So, what I want to do in the view, is firstly to loop through this array, and get the product name etc in the foreach loop. I'm a web designer who has, over the years, managed to cobble together bits of PHP code here and there, and in the bad ol' days, I'd simply have performed a sql query within the foreach loop to get this data. And, I guess, I could do this in the view file too, and it'd all work well. But that's not in the spirit of MVC, or CodeIgniter, so how the devil do I do this sort of loop "properly"?

Thanks in advance,
Phil
#2

[eluser]FinalFrag[/eluser]
So if I understand correctly what you do now is the following:

Code:
In controller:
- Get all the ID's from the database
- Send an array of ID's to the view

In view:
- Get all the info from the product with the current ID

If that is indeed what you are looking for, the solution is quite simple. In your controller, you shouldn't pass an array of ID's to your view, but an array with all the information (ID, title, description, etc). To give you an example:

Code:
// now you pass this to your view
array( 1, 2, 3 );

// you should pass this to your view
array(
    array( 1, 'shampoo', 'shampoo in a bottle' ),
    array( 2, 'soap', 'soap in a bottle' ),
    array( 3, 'toothpaste', 'in a tube' )
);

Hope it helps...
#3

[eluser]Phil Norton[/eluser]
Currently...

Quote:In Controller:
- Get a list of IDs from a serialized array in the database, as well as other info
- Sending this to the view

In View:
- Taking the array of IDs that has been sent by the controller, and in a foreach loop, getting the product name, link etc from the db in a separate query, using the product ID from the serialized array

Though what I really want is...

Quote:In Controller:
- Get a list of IDs from a serialized array in the database, as well as other info
- Using a foreach loop, take the IDs from the serialized array, and go fetch further info from the database
- Send all of this to the view

In View:
- Display the info

Hopefully this makes sense...
#4

[eluser]Phil Norton[/eluser]
[quote author="FinalFrag" date="1211345525"]To give you an example:

Code:
// now you pass this to your view
array( 1, 2, 3 );

// you should pass this to your view
array(
    array( 1, 'shampoo', 'shampoo in a bottle' ),
    array( 2, 'soap', 'soap in a bottle' ),
    array( 3, 'toothpaste', 'in a tube' )
);

Hope it helps...[/quote]

That looks pretty much like what I'm looking for. Here's a sample of what I want to achieve:

Code:
array(110,23,35); // these are the product IDs from the serialized array

array(
   array(1, 'Product Name','Product URL'), // These are taken from the db using the ID numbers above
   array(2, 'Product Name','Product URL'),
   array(3, 'Product Name','Product URL')
)

So, I guess what I'm looking for is how to create this array...
#5

[eluser]FinalFrag[/eluser]
You lost me there mate Smile

In your last post you have 110, 23 and 35 in the first array, and still you have 1, 2 and 3 in the 'full' array. Is that a typo or really what you want?

I don't see why you should have a serialised array in your database. You should have 1 row per ID. You probably have a good reason to do it this way, but I'm not familiar with this. If you could yet explain a little more...




Theme © iAndrew 2016 - Forum software by © MyBB