Jump to content
MakeWebGames

GL2 Form w/variable radio buttons


Tom V

Recommended Posts

Has anyone created a form like in this example?   Table1 and Table2 have a one to many relationship, submitted data stored in Table3.  It seems you should just be able to use {#each id} {#each option} {/each} {/each}, but that doesn't work.  Anyone have any friendly advice for me?

GL2_form.png

Link to comment
Share on other sites

$tables = array(
	array(
		"name" => "table 1", 
		"options" => array(
			array( "value" => 1, "text" => "option 1", "name" => "table1"),
			array( "value" => 2, "text" => "option 2", "name" => "table1"),
			array( "value" => 3, "text" => "option 3", "name" => "table1")
		)
	), 
	array(
		"name" => "table 2", 
		"options" => array(
			array( "value" => 1, "text" => "option 1", "name" => "table2" ),
			array( "value" => 2, "text" => "option 2", "name" => "table2" ),
			array( "value" => 3, "text" => "option 3", "name" => "table2" )
		)
	), 
	array(
		"name" => "table 3", 
		"options" => array(
			array( "value" => 1, "text" => "option 1", "name" => "table3"),
			array( "value" => 2, "text" => "option 2", "name" => "table3"),
			array( "value" => 3, "text" => "option 3", "name" => "table3")
		)
	)
);

$this->page->buildElement("tables", array(
	"tables" => $tables
));

{#each tables}
	<h1>{name}</h1>
	{#each options}
		<input type="radio" value="{value}" name="{name}[]" /> {text}
	{/each}
{/each}

Try something like this

  • Like 3
Link to comment
Share on other sites

  • 4 weeks later...

I tried to follow your example, but i can't seem to get nested each's to work.  For example, let's look at garage module.  If I want a table to look like this:

Location = England

Car 1

Car 2

Location = France

Car 3

I tried this but it doesn't give me the expected result. 

$cars[] = array(
                        "locname" =>  $loc->L_name,
                        "options" => array(
                            "name" => $car->CA_name, 
                            "location" => $loc->L_name, 
                            "damage" => $car->GA_damage.'%', 
                            "id" => $car->GA_id, 
                            "value" => number_format($value)
                        ),
                         
                        array(
                        "locname" => $loc->L_name, 
                        "options" => array(
                            "name" => $car->CA_name, 
                            "location" => $loc->L_name, 
                            "damage" => $car->GA_damage.'%', 
                            "id" => $car->GA_id, 
                            "value" => number_format($value)
                            )
                        )
                );
                
            }
            
            $this->html .= $this->page->buildElement('garage', array("cars" => $cars));


<tbody>
                            {#unless cars}
                                <tr>
                                    <td colspan="5">
                                        <div class="text-center">
                                            <em> You have no cars</em>
                                        </div>
                                    </td>
                                </tr>
                            {/unless}
                            {#each cars}
                            <h1>{locname}</h1>
                            {#each options}
                                <tr>
                                    <td>{name}</td>
                                    <td>{damage}</td>
                                    <td>${value}</td>
                                    <td>{location}</td>
                                    <td class="text-center">
                                        <a href="?page=garage&action=sell&id={id}">Sell</a>    
                                        <a href="?page=garage&action=crush&id={id}">Crush</a>    
                                        <a href="?page=garage&action=repair&id={id}">Repair</a>
                                    </td>
                                </tr>
                            {/each}
                            {/each}
                        </tbody>

 

Link to comment
Share on other sites

@Tom V Options is a flat array, it needs to be an array of arrays if that makes sense

"options" => array(
	array(
		"name" => "Car 1", 
		// ... 
	), 
	array(
		"name" => "Car 2", 
		// ... 
	), 
)

 

<?php

	$locations = $this->db->selectAll("SELECT L_name as 'name', L_id as 'id' FROM locations");

	foreach ($locations as $key => $location) {
		$locations[$key]["options"] = $this->db->selectAll("SELECT * FROM garage WHERE G_location = :loc", array(
			":loc" => $location["id"]
		));
	}

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...