Jump to content

allowing user choose from multiple rows


Cryotech
Go to solution Solved by Barand,

Recommended Posts

I know the title is a bit confusing but I wasn't exactly sure how to describe my issue and none of my numerous searches online and here returned any positive results for a solution.

What I'm trying to do is allow a user to 'choose' one set from multiple sets of numbers for inclusion into the database. The image below should with the visualization of my goal.

sets.png

 

As you can see from the above image, there's a "button" to the right that states "keep this set". My goal is to allow the user to click the button and the corresponding set of values is then entered into the database as a string (the values aren't being used in any calculations afterwards just being displayed on screen.)

The following code below works for auto-insertion if there's only one set generated but nothing I've attempted so far works when it comes to allowing the user to choose one set they'd like entered.

    <?php

additional code here...

    echo '<table><tr><td>';

    for($i=1; $i<=$Sets; $i++){
        $Stat_Results = GenStats($uId, $StartSet, $SetOption);

        echo "Stat values for ".$SetGenerated[$i];

       foreach($Stat_Results as $key => $DisplayStats){
              echo $DisplayStats.', ';
       }

       if($Sets == 1){
         $SetResults = implode(',',$Stat_Results);
         DB_Insert($uId, $SetResults, $TimeStamp);    
       }else if($Sets >= 2){
         echo "</td><td> => <input type='button' name='submit' value='keep this set'></td>
         <tr><td>";
       }
    }    
    echo '</td></tr></table>';
    ?>

html below here....

Variables: $sId, $StartSet, $SetOption, $Sets, $uId, and $TimeStamp are all set in a section not shown in this snippet.

I've tried Javascript but too be honest, I'm not all that familiar with it (though I am trying to become more fluid in it) and I've went so far as to try to create the multiple sets as a dropdown box thinking the user could simply select it from there and it be treated as a dynamic form and php would do the rest. But that didn't work either. The form button for submission is the remnant of the <select> list I tried to generate. The form is no longer there. I left the button in to generate the image you see above and has since been removed from the actual code.

I admit, I'm stuck and need some guidance because I can't seem to make this work no matter what I do. I realize $Stat_Results is a multidimensional array and contains all the values of each set in their respective array, but so far, I'm at a stand still on properly retrieving those values for insertion.

Thanks to all who read and attempt to help!

Edited by Cryotech
Link to comment
Share on other sites

If were to use a dropdown menu you need to output each generated sequence within the <option></option> tags. Example assuming GenStats() generates a new stat each time it is called

// if a stat has been submitted
if(isset($_POST['stat']))
{
	echo "The stat to insert into the database will be: " . $_POST['stat'];

	// your code for inserting stat to database here
}


// generate the stats
$Stat_Results = array();
for($i=1; $i<=$Sets; $i++)
{
	$Stat_Results[] = GenStats($SpecId, $min, $Quantity, $Type);
}

// output stats witin a HTML dropdown menu
echo '
<form action="" method="post">
	<select name="stat">
		<option>'. implode('</option><option>', $Stat_Results). '</option>
	</select>
	<input type="submit" value="Insert Selected Stat" />
</form>';
Link to comment
Share on other sites

If you want to do it with just PHP, you will need a form.  You could put the form tags around each of your submit buttons, but I would recommend putting the form tags around all of them.  If you go with the later, then you need each of your input buttons to send something unique.  Instead of using an input tag, use a button tag with the submit attribute, and include a unique value for each.  When your server gets hit, look at the value and do what you need to do.

Link to comment
Share on other sites

  • Solution

IMHO the easiest method is a form with checkboxes. The value of each checkbox would be the set (eg value='6,6,5,4,3,2');

// SAVE 'KEPT' SETS TO DATABASE
if (isset($_GET['set'])) {
    $uId = $db->real_escape_string($_GET['uid']);
    foreach ($_GET['set'] as $set) {
        $set = $db->real_escape_string($set);
        DB_Insert($uId, $set, $TimeStamp); 
    }
}

echo '<form>
        <table border="1" cellpadding="4">
        <tr><th>Set</th><th>Set Values</th><th>Keep</th></tr>';
echo "<input type='hidden' name='uid' value='$uId'>";
for($i=1; $i<=$Sets; $i++){
    $Stat_Results = GenStats($uId, $StartSet, $SetOption);
    $set = join(',', $Stat_Results);
    echo "<tr><td>$i</td><td>$set</td><td>
            <input type='checkbox' name='set[]' value='$set'>
            </td></tr>";
}
echo '</table><input type="submit" name="btnSub" value="Submit"></form>';

Link to comment
Share on other sites

Hey all,

 

Just wanted to say thanks to all that helped.

 

I went with Barand's suggestion and it worked like a charm! I did modify it a bit simply cause I'm using PDO prepared statements for insertion and queries but other than that, it does exactly as I was looking for and have been racking my brains over for a while.

 

Can't thank you all enough!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.