You are not logged in.

#1 2008-04-12 11:13:31

FeatherMonkey
Member
Registered: 2007-02-26
Posts: 313

A php problem

I'm really a novice programmer and just learning, I've encountered this problem I just can't see whats going wrong.

Firstly what I'm trying to achieve.

I wish for a submit button the links to the same page sends the content of a field which I then reiterate over to create a group of fields with remove buttons that will remove the content from the array and recreate the group. Now its more than likely what I'm doing can be achieved far easily than I'm trying if so please enlighten me.

<?php 
$rectit = $_GET['title'];
$add = $_GET['add'];
$whole = $_GET['int']." ".$_GET['meas']." ".$_GET['ingred'];
$counter = $_GET['counter'];
$del = $_GET['del'];

if ( $counter == "" ) {
    echo "not now";
    $list [] = $whole;
    unset($list);
    print_r( $list);
} else if ( $del != "" ) {
         echo "<br>del ";
         echo $del;
        unset ($list[$del]);
        $counter = $counter - 1;
        while ( $counter > 0 ) {
            $counter = $counter - 1;
            $rmve = $_GET['rmve'.$counter.''];
            echo $rmve;
            $list [] = $rmve;
        }
        print_r( $list);
} else {
    $list [] = $whole;
    echo $counter;
    while ( $counter > 0 ) {
        $counter = $counter - 1;
        $rmve = $_GET['rmve'.$counter.''];
//         echo $rmve;
        $list [] = $rmve;
    }
print_r( $list);
}
?>

<br>
<form action="" method="get">
<table border="0"><tr><td>
<table border="1"><tr><td colspan="2" align="center">Recipe Title<br><input name="title" id="title" type="text" value="<?php echo $rectit; ?>"></td></tr><tr valign="bottom"><td name="fix" align="center" valign="bottom">
<input name="ingred" id="ingred" value="" type="text">Ingredient</td></tr>
<tr>
<td align="center">

<input name="int" id="int" value="" type="text" size="5">
<select name="meas" id="meas">
<option>Cups</option>
<option>ml</option>
<option>l</option>
<option>fl oz</option>
<option>pints</option>
<option>grams</option>
<option>kilograms</option>
<option>oz</option>
<option>lb</option>
<option>each</option>
</select></td><td ><button id="add" name="add" value="add"><image src="../pics/buttons/transingred.png"/></button>
</td>
</tr>
<tr>

<?php
$counter = 0;
foreach ( $list as $value ) {
    echo "<td><input name=\"rmve$counter\" id=\"rmve\" value=\"$value\" type=\"text\"></td><td id=\"rmve\"><button id=\"rmve\" name=\"del\" value=\"$counter\"><image src=\"../pics/buttons/transrmve.png\"/></button></td></tr>";
    $counter = $counter + 1;
    echo $counter;
}

echo "<input type=\"hidden\" name=\"counter\" value=\"$counter\" />";
?>
</td></tr>
</form>
</table>

Now the above is the closest I've got but it doesn't quite do it and I'm really unsure where its going wrong I know its something to do with when I create the array. The above adds fine creates the table with the remove buttons.

It works with one if I remove it, with 2 they seem to swap around if I try the first one it deletes the second one. but any amount deletes fine if I start from the bottom/last and go up the list.

Many thanks before hand.

Offline

#2 2008-04-12 18:36:05

blu3ness
Member
From: Edmonton, Canada
Registered: 2007-12-28
Posts: 169

Re: A php problem

"I wish for a submit button the links to the same page sends the content of a field which I then reiterate over to create a group of fields with remove buttons that will remove the content from the array and recreate the group"
oh wow that's a hard to read sentence...

Instead of creating and unsetting arrays, can't you just make an associative array of keys mapping to a display flag?
And just set it to false when you get some data in _GET or _POST, (I also recommend you hide all that counter ids and stuff from the URL, use post method tongue)


Archlinux on Compaq Presario v5000 laptop smile

Offline

#3 2008-04-12 19:15:56

FeatherMonkey
Member
Registered: 2007-02-26
Posts: 313

Re: A php problem

Sorry about that was a bit of a mouthful certainly needed a few commas. wink

Thankyou for the input have had some feed back else where, this now seems to work.

<?php
print_r($_GET);
if (@$_GET["whole"]) $whole= $_GET['whole'];

if (@$_GET['title']) 
    $rectit=$_GET["title"];
else
    $rectit="";


if (@$_GET["del"] !=""){
    $todel = $_GET["del"];
    $counter = 0;
    foreach ($whole as $item) {
        if ($counter != $todel) $tmp[]=$item;
    $counter ++;
    }    
    $whole = $tmp;
}
elseif (@$_GET['add']) {
    $add = $_GET['add'];
    $whole[] = $_GET['int']." ".$_GET['meas']." ".$_GET['ingred'];
}


?>

<form action="" method="get">
<table border="0"><tr><td>
<table border="1"><tr><td colspan="2" align="center">
Recipe Title<br><input name="title" id="title" type="text" value="<?php echo $rectit; ?>"></td></tr><tr valign="bottom"><td name="fix" align="center" valign="bottom">
<input name="ingred" id="ingred" value="" type="text">Ingredient</td></tr>
<tr>
<td align="center">
<input name="int" id="int" value="" type="text" size="5">
<select name="meas" id="meas">
<option>Cups</option>
<option>ml</option>
<option>l</option>
<option>fl oz</option>
<option>pints</option>
<option>grams</option>
<option>kilograms</option>
<option>oz</option>
<option>lb</option>
<option>each</option>
</select></td><td ><button id="add" name="add" value="add"><image src="../pics/buttons/transingred.png"/></button>
</td>
</tr>
<tr>

<?php
$counter = 0;
if (@is_array($whole)){
    foreach ( $whole as $value ) {
            echo "<td><input name=\"whole[]\" value=\"$value\" type=\"text\"></td><td>";
            echo "<button name=\"del\" value=\"$counter\"><image src=\"../pics/buttons/transrmve.png\"/></button></td></tr>";
            $counter++;
    }
}
?>
</td></tr>
</form>
</table>

Still digesting the fix and thank you for the comments I very much appreciate them certainly will think of using post, for certain bits. Mainly started with GET as I could see the data being sent, combined with a lot of echo's and print_r's.

Offline

Board footer

Powered by FluxBB