swamp Posted December 18, 2008 Share Posted December 18, 2008 Hi guys, I've got this code to print a loop from a database: <?php for ($f_i=0; $f_i < $limit; $f_i++) { $data = mysql_fetch_row($result); $id = $data[0]; $section = $data[1]; $value = $data[2]; $image = $data[3]; ?> <img src="../<?php echo $image; ?>" /><br /> <?php echo $value; ?><br /> <input type="checkbox" name="format<?php echo $f_i + 1; ?>" value="<?php echo $value; ?>" id="format<?php echo $f_i + 1; ?>"/> <?php } ?> I need a to make all the ticked checkboxes into a variable.. EG: $collect_formats = $_POST['format1'] . $_POST['format2'] . $_POST['format3'] But do it as a loop again instead of having to write all of them out... etc... Thanks in advanced Link to comment https://forums.phpfreaks.com/topic/137547-solved-loop-results/ Share on other sites More sharing options...
Adam Posted December 18, 2008 Share Posted December 18, 2008 <?php $i = 1; while ($data = mysql_fetch_array($result)) { $id = $data[0]; $section = $data[1]; $value = $data[2]; $image = $data[3]; ?> <img src="../<?php echo $image; ?>" /><br /> <?php echo $value; ?><br /> <input type="checkbox" name="format<?php echo $i; ?>" value="<?php echo $value; ?>" id="format<?php echo $i; ?>"/> <?php $i++; } ?> A Link to comment https://forums.phpfreaks.com/topic/137547-solved-loop-results/#findComment-718880 Share on other sites More sharing options...
swamp Posted December 18, 2008 Author Share Posted December 18, 2008 <?php $i = 1; while ($data = mysql_fetch_array($result)) { $id = $data[0]; $section = $data[1]; $value = $data[2]; $image = $data[3]; ?> <img src="../<?php echo $image; ?>" /><br /> <?php echo $value; ?><br /> <input type="checkbox" name="format<?php echo $i; ?>" value="<?php echo $value; ?>" id="format<?php echo $i; ?>"/> <?php $i++; } ?> A ??? :/ Thats nothing to do with what I'm looking for.. Link to comment https://forums.phpfreaks.com/topic/137547-solved-loop-results/#findComment-718890 Share on other sites More sharing options...
Adam Posted December 18, 2008 Share Posted December 18, 2008 Hah sorry.. Do you mean something more like: foreach ($_POST as $val) { if (substr($val, 0, 5) == 'format') { $str .= (strlen($val) == 6) ? substr($val, 5, 1) : substr($val, 5, 2); } } Link to comment https://forums.phpfreaks.com/topic/137547-solved-loop-results/#findComment-718925 Share on other sites More sharing options...
swamp Posted December 18, 2008 Author Share Posted December 18, 2008 Hah sorry.. Do you mean something more like: foreach ($_POST as $val) { if (substr($val, 0, 5) == 'format') { $str .= (strlen($val) == 6) ? substr($val, 5, 1) : substr($val, 5, 2); } } Yeah Cheers! Thx alot. Solved. Link to comment https://forums.phpfreaks.com/topic/137547-solved-loop-results/#findComment-718930 Share on other sites More sharing options...
premiso Posted December 18, 2008 Share Posted December 18, 2008 lol or a simplier version foreach ($_POST as $val) { if (strstr($val, 'format') !== false) { $str .= str_replace("format", "", $val); } } Without all the messy substr =) Just curious though, why not make format an array of it's own? <input type="checkbox" name="format[]" value="<?php echo $value; ?>" id="format<?php echo $f_i + 1; ?>"/> Then you could just access it like: if (isset($_POST['format'])) { foreach ($_POST['format'] as $val) { $str .= $val; } } That way you do not have to worry about appending numbers to it etc =) Link to comment https://forums.phpfreaks.com/topic/137547-solved-loop-results/#findComment-718936 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.