Welcome Guest, Not a member yet? Register   Sign In
Select box with names
#11

[eluser]jwindhorst[/eluser]
If the data maps up the way that I assume it does, then the join is necessary. It is entirely possible that you don't need that depending on what your data needs to do in the end.

The real point I was trying to make, is that instead of trying to nest loops in PHP, perhaps you can find a way to get the data in a more convenient format from the DB.

CI by the way shouldn't have changed the result set you get from your DB Wink
#12

[eluser]jedd[/eluser]
That wasn't me, you know. {points}

Hey, with the subtype being the ID in the form, how do you know what substrate you selected ..? Or do you simply not care? I'm assuming that subtype ID's can occur multiple times, under different substrate headings .. ? Or is that wrong.

You could have both bits of data in your ID, you know, and split them later .. that's quite achievable, if you need to identify both substrate and subtype later, from the form selection.

The SQL that jwindhorst offered is pretty much what I was heading towards.

I tend to eschew the this->db->where/order/etc stuff still - far more comfortable with raw SQL calls.

IFF I have understood your schema properly, you might want to play with something like this (in the model)

Code:
$query = $this->db->query ("SELECT subtype.id AS subtype_id,
                                   substrate.id  AS  substrate_id,
                                   substrate.substrate  AS  substrate_name,
                                   subtype.subtype  AS  subtype_name
                            FROM subtype
                            LEFT JOIN
                                    substrate ON subtype.ssid=substrate.id
                            ORDER BY
                                   subtype.subtype ASC");
$results = $query->result_array();

You might want to var_dump this, in your controller, to get an idea of what you are playing with. It will still need some massaging, of course, and this code is not tested.
#13

[eluser]mdcode[/eluser]
I'm so close yet so far...

Incorporating the original query from the old system, I am able to echo out the results at the top of the page like this:

Code:
BoardBatabakBoardBatstarPaperCoatedPaperThermalPaperUncoated

These are the correct reults in the order specified by the query, but they will not populate the select box.

My MODEL:
Code:
function get_linked_substrates()
            {
                $query = $this->db->query("SELECT a.id, a.substrate, b.id, b.ssid, b.subtype FROM lip_substrates AS a, lip_subtypes AS b WHERE a.id = b.ssid ORDER BY a.substrate, b.subtype ASC");
                foreach ($query->result_array() as $row)
                {
                echo $row['substrate'];
                echo $row['subtype'];
                }
            }

My CONTROLLER:
Code:
/* grab the linked substrates list */
            $substrates = $this->projects_model->get_linked_substrates();
            {
                foreach ($substrates as $key=>$value)
                $data['substrates'][$value['id']] = $value['substrate'] ." - ". $value['subtype'];
            }

Any further ideas are appreciated as it's possible I'm again being quite dense.
#14

[eluser]mdcode[/eluser]
Oops, sorry jedd and jwindhorst, I really should pay more attention. I should have also said in the previous post that I'm getting this error:

Code:
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: controllers/projects.php
Line Number: 93
and this one next to the select box:
Code:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: substrates
Filename: admin/projects_add.php
Line Number: 111

The above code from the controller and model is still the same at present.
#15

[eluser]jedd[/eluser]
Your model method does not appear to have a return statement. Useful thing to put back in there!

Also, your controller should test this. I think your indenting is a little bit skew, too. I'd change your controller snippet to:

Code:
/* grab the linked substrates list */
     if ($substrates = $this->projects_model->get_linked_substrates() )
           foreach ($substrates as $key=>$value)
                 $data['substrates'][$value['id']] = $value['substrate'] ." - ". $value['subtype'];
#16

[eluser]mdcode[/eluser]
True... but I can only have one return statement there, what happens when I need to return multiple values?
#17

[eluser]jedd[/eluser]
Multiple values? That's what arrays are for.

Consider the code I posted earlier (note that I also updated the most recent post of mine - refresh to have a read of it - with a code change for your controller).

Your model should have, after either of the two offered SQL SELECT calls, these lines:
Code:
$results = $query->result_array();
return $results;

You shouldn't have echos in the model.
#18

[eluser]mdcode[/eluser]
If I have "return $row;" in there, this is how the select box is rendered:
Code:
<select name="substrate" style='width:160px;'>
<option value="4">4 - 4</option>
<option value="B">B - B</option>
<option value="2">2 - 2</option>
</select>
I have no idea what's going on with the values, and there should be 5 results there.
#19

[eluser]mdcode[/eluser]
Thank you, thank you, thank you!!! To you both. I have been trying to get this for almost a week now and while this is not quite perfect, it's mucho acceptable. Another fine job, thanks jedd, and dare I say it, with this one, I'm understanding that little bit more. Smile
#20

[eluser]jedd[/eluser]
You've returned a row - which is not a set of rows (as per your 5 expected results) - just one row. So one result.

Some generic advice here - start putting in
Code:
echo "<pre>";
echo var_dump ($insert_confusing_variable_here) ;
echo "</pre>";

snippets everywhere - and look at the different variables you get out of databases - it'll make so much more sense once you see, and get used to, the nature of the data that you're playing with, and what the different CI functions actually do to your data.




Theme © iAndrew 2016 - Forum software by © MyBB