Jump to content

[SOLVED] Loop results


swamp

Recommended Posts

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

<?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

<?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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.