Page 1 of 1

Forms and Multiple Arrays If you can help, I will love ya :P

Posted: Wed Aug 14, 2002 3:17 am
by MAsKrA
Ok So forget all I said before this is my prob, I have to arrays, that are passed using a checkbox on a form. One been a url, the next one been the value:

IE
theurl = /index.php
value=23424
Cant figure out how to grab that information from that array ($theurl $value) keep it together in other words the first theurl goes with the first value.

This works:

Code: Select all

foreach($theurl as $url)
{
                print "$url\n";
}
But I need both $theurl and $value, so I can insert it into a gdbm file.

Posted: Wed Aug 14, 2002 9:12 am
by aaronhall
I'm not sure that I understand what you are trying to do. Please clarify - maybe I can help ;)

-Aaron

Posted: Wed Aug 14, 2002 9:14 am
by llimllib
are you getting error messages? is it not doing what you need it to? explain, more clearly, what you're trying to accomplish, and we'll hook you up.

Posted: Wed Aug 14, 2002 7:59 pm
by MAsKrA
Damn I should stop typing after 3 am, Ok so here it goes, If you go here:

http://gamedestroyers.com/dev/counter/

I need to be able to select more than one checkbox, input more than one value in my text box, click change and insert the information in to a gdbm binary file. I have my checkbox and value boxes named like so:

Code: Select all

// $val is the url to the page with the counter ie /dev/counter/index.php

<input type=checkbox name=theurl&#1111;] value=$val>$val</td>
<input type=text name=value&#1111;]>
Now when I process my form I cant figure out how to pack that information and insert it to the gdbm file:

Code: Select all

elseif ( ($mode == "Change") && ($theurl) && ($value) )
   &#123; 
      //
      // We are going to change the count for a current counter.
      //
         $dd=dba_open("$counter",'w','gdbm') || print "No dbm\n";
		 $url = implode(",",$theurl);
		 $newcount = implode(",",$value);
         $now=time();
		 $packed=pack("ii",$newcount,$now);
		 echo $url;
		 echo $newcount;
         print("<center><strong>changed the url $url to $newcount at $now</strong></center><BR>\n");
         dba_replace($url,$packed,$dd);
Now that I am using the implode(",",$theurl); I can acutally get it to insert in to one value, I just cant figure out how to make a loop so while I have a valid $theurl and a valid $value, I do the dba_replace for each.

Did I even said that correctly?

Posted: Wed Aug 14, 2002 8:01 pm
by MAsKrA
And yes the problem is, that is not inserting the information for more than one.

Thanks
:twisted:

Posted: Thu Aug 15, 2002 1:52 am
by aaronhall
Thank you mufasa, or mukasfra, or mukaska, orr.. whatever ;)

Anyway, I'm not even going to read through that code becuase I'm too damn lazy. I say, write some code to track how many counters need to be changed and just make a for loop that goes through each change and edits the file. If this is what you were already doing, please ignore this message. Good luck

-Aaron

Posted: Thu Aug 15, 2002 9:31 pm
by MAsKrA
Updated information.

Ok So forget all I said before this is my prob, I have to arrays, that are passed using a checkbox on a form. One been a url, the next one been the value:

IE
theurl = /index.php
value=23424
Cant figure out how to grab that information from that array ($theurl $value) keep it together in other words the first theurl goes with the first value.

This works:

Code: Select all

foreach($theurl as $url)
&#123;
                print "$url\n";
&#125;
But I need both $theurl and $value, so I can insert it into a gdbm file.

Posted: Fri Aug 16, 2002 1:43 am
by twigletmac
Your form may need a bit of reorganising, what does it look like?

Mac

Posted: Fri Aug 16, 2002 4:07 am
by MAsKrA
Here is the section with my entire for:

Code: Select all

<Form method=post action="<? echo $PHP_SELF; ?>">
        <?php
        for ($i=0;$i<count($mall); $i++)
         &#123;
          $val = $mall&#1111;$i];
          $key = dba_fetch($val,$dd);
          $splitkey = unpack("i2int",$key);
          $count = $splitkey&#1111;int1];
          $changetime = $splitkey&#1111;int2];
          print "<tr><td><input type=checkbox name=theurl&#1111;] value=$val>$val</td><td>$count</td><td><input type=text name=value&#1111;]></td></tr>";
         &#125;;
        ?>	
	</table><br>
	<table border="1">
	  <tr>
        <td align="center" valign="middle"><input type=submit value=Add name=mode></td>
        <td align="center" valign="middle"><input type=submit value=Delete name=mode></td>
        <td align="center" valign="middle"><input type=submit value=Change name=mode></td> 
   	  </tr>
		</Form>

Posted: Fri Aug 16, 2002 4:23 am
by MAsKrA
I also tried this vor my arrays again I get it to work fine with just one like here $theurl, but cant figure out how to add the same for $value

Code: Select all

for ($i=0;$i<each($theurl); $i++)
		&#123;
		 $url = $theurl&#1111;$i];
		 $cnt = $value&#1111;$i];
		 $packed=pack("ii",$value,$now);
                 print("<center><strong>changed the url $url to $cnt at $now</strong></center><BR>\n");
         	 //dba_replace($theurl,$packed,$dd);
		&#125;

Posted: Fri Aug 16, 2002 4:42 am
by hob_goblin

Code: Select all

foreach($theurl as $key => $data)&#123;
  echo $data;
  echo $value&#1111;$key];
  &#125;
do you know how to use foreach loops like that?

Posted: Fri Aug 16, 2002 4:48 am
by twigletmac
How about:

Code: Select all

echo '<tr><td><input type="checkbox" name="theurl&#1111;'.$i.']" value="'.$val.'">'.$val.'</td><td>'.$count.'</td><td><input type="text" name="value&#1111;'.$i.']"></td></tr>";
Then the two values share a common associative key for sure and you can use hob_goblin's code to print out the associated pairs (and do other stuff within the foreach() loop of course).

Mac

Posted: Fri Aug 16, 2002 5:43 am
by MAsKrA
OMG THANK YOU THANK YOU THANK YOU!!!!

A Combination of those 2, did the trick for me, this is what I ended up with:

Code: Select all

print "<tr><td><input type=checkbox name=theurl&#1111;'.$i.'] value=$val>$val</td><td>$count</td><td><input type=text name=value&#1111;'.$i.']></td></tr>";
And:

Code: Select all

foreach($theurl as $key => $data)
		 &#123;  
		    $packed=pack("ii",$value&#1111;$key],$now);
             print("<center><strong>changed the url $data to $value&#1111;$key] at $now</strong></center><BR>\n");
         	 dba_replace($data,$packed,$dd);
		 &#125;

Again Thanks This place rocks!!! :twisted: