sheriff Posted April 17, 2007 Share Posted April 17, 2007 hi all ! I have a textfield like: <input type="text" name="cant[]"> on the next page i have 10 textfields like this .. so i have an $cant array. how can I list the value of them from the array ? with foreach I suppose but how ? Thanks and sorry for the beginner question! Link to comment https://forums.phpfreaks.com/topic/47359-little-help-with-array/ Share on other sites More sharing options...
trq Posted April 17, 2007 Share Posted April 17, 2007 <?php if (isset($_POST['cant'])) { foreach($_POST['cant'] as $cant) { echo $cant."\n"; } } ?> Link to comment https://forums.phpfreaks.com/topic/47359-little-help-with-array/#findComment-231043 Share on other sites More sharing options...
jitesh Posted April 17, 2007 Share Posted April 17, 2007 <?php echo "<pre>"; print_r($_POST['cant']); //You can use foreach foreach($_POST['cant'] as $key => $value){ echo $key ."---".$value; } //Or you can use for for($i=0;$i<count($_POST['cant']);$i++){ echo $_POST['cant'][$i]; } ?> Link to comment https://forums.phpfreaks.com/topic/47359-little-help-with-array/#findComment-231052 Share on other sites More sharing options...
sheriff Posted April 17, 2007 Author Share Posted April 17, 2007 little problem ... this textfield is in a table what is generated by php from mysql. after is generated, i enter in this textfield numbers what is the quantity of each product. the data is sent by the script you tell'd me, but only the last number in the table. exemple: <table bgcolor="#000000" cellpadding="1" cellspacing="1" width="78%"> <tr> <td width="28%" bgcolor="#000000" align="center"><font color="#FFFFFF" size="3"><strong>Nume</strong></font></td> <td width="60%" bgcolor="#000000" align="center"><font color="#FFFFFF" size="3"><strong>Descriere</strong></font></td> <td width="5%" bgcolor="#000000" align="center"><font color="#FFFFFF" size="3"><strong>UM</strong></font></td> <td width="7%" bgcolor="#000000" align="center"><font color="#FFFFFF" size="3"><strong>Cant</strong></font></td> </tr> <tr> <td bgcolor="#DDD7CC"><font size="3">product 1</font></td> <td bgcolor="#DDD7CC"><font size="3">size and stuff</font></td> <td align=center bgcolor="#DDD7CC"><font size="3">buc</font></td> <td align=right bgcolor="#DDD7CC"><font size="3"></font>3</td> </tr> <tr> <td bgcolor="#DDDAD4"><font size="3">product 2</font></td> <td bgcolor="#DDDAD4"><font size="3">size and stuff</font></td> <td align=center bgcolor="#DDDAD4"><font size="3">buc</font></td> <td align=right bgcolor="#DDDAD4"><font size="3"></font>3</td> </tr> </table> Link to comment https://forums.phpfreaks.com/topic/47359-little-help-with-array/#findComment-231055 Share on other sites More sharing options...
jitesh Posted April 17, 2007 Share Posted April 17, 2007 <table bgcolor="#000000" cellpadding="1" cellspacing="1" width="78%"> <tr> <td width="28%" bgcolor="#000000" align="center"><font color="#FFFFFF" size="3"><strong>Nume</strong></font></td> <td width="60%" bgcolor="#000000" align="center"><font color="#FFFFFF" size="3"><strong>Descriere</strong></font></td> <td width="5%" bgcolor="#000000" align="center"><font color="#FFFFFF" size="3"><strong>UM</strong></font></td> <td width="7%" bgcolor="#000000" align="center"><font color="#FFFFFF" size="3"><strong>Cant</strong></font></td> </tr> <?php foreach($_POST['cant'] as $key => $value){ ?><tr> <td bgcolor="#DDD7CC"><font size="3">product 1</font></td> <td bgcolor="#DDD7CC"><font size="3">size and stuff</font></td> <td align=center bgcolor="#DDD7CC"><font size="3">buc</font></td> <td align=right bgcolor="#DDD7CC"><font size="3">$value</font>3</td> </tr> <? } ?> </table> Link to comment https://forums.phpfreaks.com/topic/47359-little-help-with-array/#findComment-231058 Share on other sites More sharing options...
sheriff Posted April 17, 2007 Author Share Posted April 17, 2007 it's work now, but because I already have a foreach he double the table, if(isset($_POST)){ if(count($_POST['check'])){ $list_selected = ""; foreach($_POST['check'] as $key => $ids){ $list_selected .= $ids .","; } foreach($_POST['cant'] as $key => $value) $list_selected = substr($list_selected,0,strlen($list_selected)-1); // This will give ur selected ids like this 100,203,125,123 }else{ $list_selected = "-1"; } } how can i add this ($_POST['cant'] as $key => $value) too ? thanks Link to comment https://forums.phpfreaks.com/topic/47359-little-help-with-array/#findComment-231064 Share on other sites More sharing options...
jitesh Posted April 17, 2007 Share Posted April 17, 2007 Use For loop for($i=0;$i<count($_POST['check']);$i++){ echo $_POST['check'][$i]; echo $_POST['cant'][$i]; } Link to comment https://forums.phpfreaks.com/topic/47359-little-help-with-array/#findComment-231070 Share on other sites More sharing options...
sheriff Posted April 17, 2007 Author Share Posted April 17, 2007 so ... I have if(isset($_POST)){ if(count($_POST['check'])){ $list_selected = ""; foreach($_POST['check'] as $key => $ids){ $list_selected .= $ids .","; } $list_selected = substr($list_selected,0,strlen($list_selected)-1); // This will give ur selected ids like this 100,203,125,123 } }else{ $list_selected = "-1"; } } how i implement your code ? if(isset($_POST)){ if(count($_POST['check'])){ $list_selected = ""; for($i=0;$i<count($_POST['check']);$i++){ echo $_POST['check'][$i]; echo $_POST['cant'][$i]; } $list_selected = substr($list_selected,0,strlen($list_selected)-1); // This will give ur selected ids like this 100,203,125,123 } in this way don't work Sorry ... i'm noob Link to comment https://forums.phpfreaks.com/topic/47359-little-help-with-array/#findComment-231084 Share on other sites More sharing options...
jitesh Posted April 17, 2007 Share Posted April 17, 2007 if count for $_POST['check'] and $_POST['cant'] are same then <tr> <td width="28%" bgcolor="#000000" align="center"><font color="#FFFFFF" size="3"><strong>Nume</strong></font></td> <td width="60%" bgcolor="#000000" align="center"><font color="#FFFFFF" size="3"><strong>Descriere</strong></font></td> <td width="5%" bgcolor="#000000" align="center"><font color="#FFFFFF" size="3"><strong>UM</strong></font></td> <td width="7%" bgcolor="#000000" align="center"><font color="#FFFFFF" size="3"><strong>Cant</strong></font></td> </tr> <?php for($i=0;$i<count($_POST['check']);$i++){ ?> <tr> <td bgcolor="#DDD7CC"><font size="3">product 1</font></td> <td bgcolor="#DDD7CC"><font size="3">size and stuff</font></td> <td align=center bgcolor="#DDD7CC"><font size="3"><?php echo $_POST['check'][$i];?></font></td> <td align=right bgcolor="#DDD7CC"><font size="3"><?php echo $_POST['cant'][$i];?></font>3</td> </tr> <?php } ?> Link to comment https://forums.phpfreaks.com/topic/47359-little-help-with-array/#findComment-231135 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.