Welcome Guest, Not a member yet? Register   Sign In
help with looping query XD
#1

[eluser]KrizzAngel[/eluser]
ok i got this on my model:
Quote:$segment=$this->input->post('seg');
for($z=1;$z<=$segment;$z++){
$lessNum$z=$this->input->post('lessonNum$z'); <-- i cant get this variable
$lessTit$z=$this->input->post('lessontitle$z'); <-- i cant get this variable
$con$zt=$this->input->post('content$z'); <-- i cant get this variable
$grad=$this->input->post('Grade');
$chaps=$this->input->post('chapter_no');
$this->db->query("INSERT INTO tbltopic (lesson_no,lesson_name,content) VALUES ('$lessNum$z','$lessTit$z','$cont$z')");
$this->db->query("INSERT INTO tblevaluation (chapter_no,grade_no) VALUES ('$chaps','$grad')");

then on my view:
Quote:&lt;? echo form_open('admin/add'); ?&gt;

&lt;? $seg=$this->uri->segment(3);
echo "Grade : "; ?&gt;<select name="Grade"><option selected="selected">-Grade-</option>
&lt;? for($a=1; $a<=6 ; $a++)
{ ?&gt;
<option value="&lt;? echo $a; ?&gt;" >&lt;? echo $a; ?&gt;</option>
&lt;? } ?&gt;
</select>
&lt;? $this->db->select('chapter_no');
$this->db->from('tbltopic');
$num=$this->db->count_all_results();
if($num >= '1'){
$this->db->select_max('chapter_no');
$cha=$this->db->get('tbltopic');
foreach ($cha->result() as $chap)
{
$chapter= ($chap->chapter_no)+1;
echo "<br />Chapter ".$chapter.":";
?&gt;&lt;input type="hidden" value="&lt;? echo $chapter; ?&gt;" name="chapter_no" /&gt;&lt;?
}
}
else
{ echo "<br />Chapter 1:"; ?&gt;
&lt;input type="hidden" value="1" name="chapter_no" /&gt; &lt;?
}
?&gt;

Title: &lt;input type="text" name="chapter" /&gt;&lt;br />

&lt;?
if (isset($_REQUEST['waa']))
$seg=$_REQUEST['Go']+$_REQUEST['waa']; ?&gt;
&lt;input type="hidden" name="seg" value="&lt;? echo $seg; ?&gt;" /&gt;
&lt;?
for ($x=1 ; $x<=$seg ; $x++){
echo "Lesson $x: <br>";
?&gt; &lt;input type="hidden" name="lessonNum&lt;? echo $x; ?&gt; " value="&lt;? echo $x; ?&gt;" /&gt;
Lesson Title: &lt;input type="text" name="lessontitle&lt;? echo $x; ?&gt;" /&gt;&lt;br />
Upload Video: &lt;input type="file" name="video" /&gt;
&lt;?
$y= "content";
?&gt;&lt;textarea name="content&lt;? echo $x; ?&gt;" rows="15" cols="80" style="width: 80%"&gt;
&lt;/textarea&gt;
&lt;?
}
?&gt;
<br />
&lt;input type="submit" name="save" value="Submit" /&gt;
&lt;input type="reset" name="reset" value="Reset" /&gt;

&lt;/form&gt;or&lt;form action="&lt;? anchor('admin/test/'.$this-&gt;uri->segment(3)+$this->input->post('waa')) ?&gt;"> &lt;input type="text" name="waa" value="1" /&gt; &lt;input type="hidden" name="Go" value="&lt;? echo $seg; ?&gt;" /&gt; &lt;input type="submit" value="Go" /&gt;&lt;/form>

any suggestion for looping query?? i really dont know how em i gonna use or retrieve the < name tag > of the looped text/textarea
#2

[eluser]theprodigy[/eluser]
Quote:for($z=1;$z<=$segment;$z++){
$lessNum$z=$this->input->post(‘lessonNum$z’); <—i cant get this variable
$lessTit$z=$this->input->post(‘lessontitle$z’); <—i cant get this variable
$con$zt=$this->input->post(‘content$z’); <—i cant get this variable
$grad=$this->input->post(‘Grade’);

try
Code:
for($z=1;$z<=$segment;$z++){
    $lessNum$z=$this->input->post('lessonNum' . $z);
    $lessTit$z=$this->input->post('lessontitle' . $z);
    $con$zt=$this->input->post('content' . $z);
    $grad=$this->input->post(‘Grade’);
//rest of code here
#3

[eluser]KrizzAngel[/eluser]
but is it ok to assign a variable (ex. $content) plus the variable on the loop which is $z so it would be $content$z?
#4

[eluser]theprodigy[/eluser]
You can (I believe). But you may have better luck using array field names

Instead of:
Quote:&lt;?
for ($x=1 ; $x<=$seg ; $x++){
echo “Lesson $x:
“;
?&gt; &lt;input type=“hidden” name=“lessonNum&lt;? echo $x; ?&gt; ” value=”&lt;? echo $x; ?&gt;” /&gt;
Lesson Title: &lt;input type=“text” name=“lessontitle&lt;? echo $x; ?&gt;” /&gt;&lt;br >
Upload Video: &lt;input type=“file” name=“video” /&gt; // <--------------by the way, did you miss one??
&lt;?
$y= “content”;
?&gt;&lt;textarea name=“content&lt;? echo $x; ?&gt;” rows=“15” cols=“80” style=“width: 80%”&gt;
&lt;/textarea&gt;
&lt;?
}
?&gt;
try
Quote:&lt;?
for ($x=1 ; $x<=$seg ; $x++){
echo “Lesson $x:
“;
?&gt; &lt;input type=“hidden” name=“lessonNum[]” value=”&lt;? echo $x; ?&gt;” /&gt;
Lesson Title: &lt;input type=“text” name=“lessontitle[]” /&gt;&lt;br >
Upload Video: &lt;input type=“file” name=“video” /&gt; // <-------------- miss one?
&lt;?
$y= “content”;
?&gt;&lt;textarea name=“content[]” rows=“15” cols=“80” style=“width: 80%”&gt;
&lt;/textarea&gt;
&lt;?
}
?&gt;
#5

[eluser]KrizzAngel[/eluser]
parse error on line 16 where my first post "$lessNum$z=$this->input->post('lessonNum' . $z);" resides. :/ dunno how to catch the looped name of the form from my view huhu
#6

[eluser]KrizzAngel[/eluser]
oh bout ur post above the i didnt put any code on that "file" type yet so i leave it blank/no name. why []?? on my model how can i catch that??
#7

[eluser]theprodigy[/eluser]
To be honest, I'm not really sure how you would catch it, as I personally have never used it that way, but I believe (by reading through the form validation section in the user guide), you would catch it as:
Code:
$this->input->post('lessontitle[]');

I only say that because in the form validation section, it says that in order to run validation on the field you have to specify it that way. It would only be logical that you would have to catch it that way. After successfully catching it, I believe you would have an array for a variable, but I would run a print_r on it first.
Code:
$title = $this->input->post('lessontitle[]');
print_r($title);
#8

[eluser]theprodigy[/eluser]
doing it that way, you won't have to worry about trying to create variable variables. But in case you still want to do it that way, you may want to take a look at:
http://php.net/manual/en/language.variab...riable.php
#9

[eluser]KrizzAngel[/eluser]
after using that [].. i still recieving parse error.. :/




Theme © iAndrew 2016 - Forum software by © MyBB