Loopy Php Code

liunx

Guest
I'm just trying to check the logic of this idea, before I have to sit down and dump it in a much more complicated form.<br /><br />Say I have a set of data associated with links, and this data gets saved into a link table in my database. To edit the data, I have a form that pulls the information out of the database and outputs it something like this:<br /><br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1--><input type="hidden" name="url_id" value="1" /><br /><input type="text" name="category" value="homepage" /><br /><input type="text" name="url" value="homepage.com" /><br /><input type="text" name="linktext" value="Home Page" /><!--c2--></div><!--ec2--><br /><br />Updating a single link isn't a problem. But what if I have a variable number of links, tied to an author ID? The author ID is defined elsewhere on the form, and doesn't play into this except that for one author I might need to update six links and for another eight--so the number of links will change constantly.<br /><br />Can I turn "hidden" and "text" fields into arrays like I would use radio buttons or check boxes? So the code would look like this:<br /><br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1--><input type="hidden" name="url_id[]" value="1" /><br />... etc<!--c2--></div><!--ec2--><br /><br />And then on the results page, I should just have to count up the number of items in one of the arrays (say url_id[]) and then use a loop like:<br /><br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->for ($a=0; $a<$count; $a++) {<br />update database, setting category=$category[$a], url=$url[$a], linktext=$linktext[$a] where url_id = $url_id[$a]<br />}<!--c2--></div><!--ec2--><br /><br />I know that's not valid PHP/MySQL, but is the logic right? It wouldn't matter how many sets of link data I have, because each element would be its own array. But will the data be stored in the correct order? When it gets to $a=7, will the seventh value in each array all correspond to the seventh link? <br /><br />Everything else I've tried isn't working, and I'm tired of testing broken code. If anyone can confirm the logic of this looks good, at least I'll know that, *in theory*, I have a workable model. It'll make error-fixing more tolerable... [I'm not the least bit frustrated... not me. No way.]<!--content-->
looks fine to me.<!--content-->
 
Back
Top