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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
MAsKrA
Forum Newbie
Posts: 14
Joined: Wed Aug 14, 2002 3:17 am
Location: Orl FL

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

Post 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.
Last edited by MAsKrA on Fri Aug 16, 2002 4:25 am, edited 4 times in total.
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

I'm not sure that I understand what you are trying to do. Please clarify - maybe I can help ;)

-Aaron
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post 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.
MAsKrA
Forum Newbie
Posts: 14
Joined: Wed Aug 14, 2002 3:17 am
Location: Orl FL

Post 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?
MAsKrA
Forum Newbie
Posts: 14
Joined: Wed Aug 14, 2002 3:17 am
Location: Orl FL

Post by MAsKrA »

And yes the problem is, that is not inserting the information for more than one.

Thanks
:twisted:
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post 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
MAsKrA
Forum Newbie
Posts: 14
Joined: Wed Aug 14, 2002 3:17 am
Location: Orl FL

Post 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.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Your form may need a bit of reorganising, what does it look like?

Mac
MAsKrA
Forum Newbie
Posts: 14
Joined: Wed Aug 14, 2002 3:17 am
Location: Orl FL

Post 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>
MAsKrA
Forum Newbie
Posts: 14
Joined: Wed Aug 14, 2002 3:17 am
Location: Orl FL

Post 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;
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post 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?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
MAsKrA
Forum Newbie
Posts: 14
Joined: Wed Aug 14, 2002 3:17 am
Location: Orl FL

Post 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:
Post Reply