How can i get this done using php

Discussion in 'Programming' started by mumfry, Nov 30, 2014.

  1. #1
    So i will get right into it.

    I have a form that looks like this
    <form>
    
    <div class="section1">
    <input name="singletitle[]"/>
    <input name="urls[]"/>
    <input name="urls[]"/>
    <input name="urls[]"/>
    <input name="urls[]"/>
    </div>
    
    <div class="section2">
    <input name="singletitle[]"/>
    <input name="urls[]"/>
    <input name="urls[]"/>
    </div>
    
    <div class="section3">
    <input name="singletitle[]"/>
    <input name="urls[]"/>
    </div>
    </form>
    HTML:
    What i would like to do is get the urls for each section, also the number of url fields for each section is always dynamic. So there is no telling how many might be there at given time

    I was thinking that i can maybe do this, add another input for each section that will keep account of the number of urls in each section then use that number as a way of dividing the urls, it would look something like this.

    
    $singletitle=$_POST['singletitle'];
    foreach($singletitle as $key=>value)
    {
    $start_for_urls=0;
    $number_of_url_fields_each_section=value_from_form;
    for($start_for_urls <= $number_of_url_fields_each_section ; $start_for_urls++){
    $get_ech_url= value_from_form[$start_for_urls];
    }
    }
    PHP:
    Seems like much, is there a better or more simpler way of getting this done

    Any help would be greatly appreciated
    Thanks
     
    Solved! View solution.
    mumfry, Nov 30, 2014 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    Why don't you just make the arrays multidimensional?
    urls[1][]...urls[1][]
    urls[2][]...urls[2][]
    And you could also do
    singletitle[1][] and they will coher to the select array
     
    PoPSiCLe, Nov 30, 2014 IP
    sarahk likes this.
  3. mumfry

    mumfry Active Member

    Messages:
    118
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #3
    Oh my goodness...
    Never thought of that in a million years.
    Simply brilliant.
    Saved me a lot of time.

    Thank you so very much
    :)

    but i noticed one thing, that you had singletitle[][] (multidentional)... is it a must that i make singletitle multidimensional as well
     
    mumfry, Nov 30, 2014 IP
  4. #4
    I'd take it even further, and use 'section' as the named reference...

    <fieldset class="section1">
    	<input name="section[1][title]"/>
    	<input name="section[1][url][]"/>
    	<input name="section[1][url][]"/>
    	<input name="section[1][url][]"/>
    	<input name="section[1][url][]"/>
    </fieldset>
     
    <fieldset class="section2">
    	<input name="section[2][title]"/>
    	<input name="section[2][url][]"/>
    	<input name="section[2][url][]"/>
    </fieldset>
     
    <fieldset class="section3">
    	<input name="section[3][title]"/>
    	<input name="section[3][url][]"/>
    </fieldset>
    Code (markup):
    That way server side to pull them all you need to do is this:

    foreach ($_POST['section'] as $section) {
    	echo '
    		<h2>', $section['title'], '</h2>
    		<ul>';
    	foreach ($section['url'] as $url) {
    		echo '
    			<li>', $url,' </li>';
    	}
    	echo '
    		</ul>';
    }
    Code (markup):
    I echoed out the values just to show how that works. You seemed to be getting WAY too complex in your processing there, and I'm not sure to what end other than making "variables for nothing".
     
    deathshadow, Nov 30, 2014 IP
  5. mumfry

    mumfry Active Member

    Messages:
    118
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #5
    Hello shadow.
    Thanks for the reply, your way is a little different than what am used to but i give it a go see.
    Thanks
     
    mumfry, Nov 30, 2014 IP