I have a MySQL database that stores items (goods and services), and along with each item, terms (terms and conditions) will also be stored, which can be roughly 2000 bytes.
http://i.imgur.com/7t3cvSE.png
This is my basic set up
$term_options = array();
$term_options[] = "None";
$term_options[] = "massive string containing 2000 bytes or more";
...
foreach ($term_options as $term) {
?>
<label class="term">
<input type="radio" name="terms" value="<?php echo $term; ?>">
<?php echo $term; ?>
</label>
<?php
}
The above code does exactly what I want, but it feels wrong to have a massive value within a radio input (which may contain Unicode characters). Am I wrong to worry?
Before, I used a SELECT menu with option values equal to the index position of each array item:
?>
<select name="terms">
<?php
$i = 0;
for ($i = 0; $i < count($term_options); $i++) {
?>
<option><?php echo $i; ?></option>
<?php
}
?>
</select>
<?php
Then my $_POST would look something like this:
$terms = $term_options[$_POST['terms']];
It worked nicely until it came to my update form which should display the currently selected values. I wasn't sure how to compare the isset value with something generated via array.
It's simple to do this with static values, e.g.:
<option <?php if (isset($row['x']) && $row['x'] === 'x') echo 'selected'; ?>>x</option>
but while creating each option in a foreach loop, I have no idea what to do and the below doesn't work:
<option <?php if (isset($row['x']) && $row['x'] === $term_options[$i]) echo 'selected'; ?>><?php echo $i; ?></option>
$i++;
Yes, you are right in your worries. If this page is going to display many of these items and each item has a massive string like this, the final page is going to be huge. An this page will be sent by your server to the client browser. Bigger the page, bigger the time.
You are retrieving these items from a MySQL database. Each item probably has a primary key. Why don't you just use this primary key as part of the name of the radio buttons and then you may retrieve again just the selected item?
Something like
<input type="radio" name="rb_1">
where this "1" appended to "rb_" is the key of an item. Then you just need to break this string, recover the primary key and search if to recover the huge string.
This may generate another database operation, of course. But this will be nothing when compared with the transmission of a huge page with, say, 100 times 2k items.
Related
I am a beginner trying to ouptut the values of dropdown menu from a database. I already automatically generate the values from database but the problem i am having is I want the the value that is being selected to be displayed.
Ideally im trying to update a Subject which I want to data to be displayed in an HTML page where each item could be updated. What i need is to be able to select the 'position' of the subject that is beeing selected.
Here is my code:
<p>Position:
<select name="position">
<?php
$sel_subject = get_all_subjects();
$subject_count = mysql_num_rows($sel_subject);
//$subject_count+1 because we are adding a subject
for($count=1; $count <= $subject_count+1; $count++) {
echo "<option value=\"{$count}\"";
if($sel_subject['position'] == $count) {
echo " selected='selected'";
}
echo ">{$count}</option>";
}
?>
</select>
You seem to be relying on $count to be some internal ID that remains static; given that it's just a monotonically advancing integer, this seems like a risky proposition. However, if you're comfortable with that, all you need to do is change the value in the last echo statement to be the subject name instead of $count. I would instead encourage you to use something meaningful to the database - for example, some people create an auto_increment field called subject_id and key off of that; others like to use UUIDs.
I would also generally suggest using a foreach loop instead of a for loop, as it tends to simplify the code (you don't have to worry about maintaining the counter, or creating fence post errors, etc.) Here's a brief example - I'm guessing a bit at what the actual data is that is available to you, hopefully you can extrapolate it to what you actually have.
<p>Position:
<select name="position">
<?php
$sel_subject = get_all_subjects();
foreach($sel_subject as $subject_num => $subject) {
echo '<option value="', $subject_num, '"';
if ($subject['selected']) {
echo 'selected="selected"';
}
echo '>', $subject['subject_name'], '</option>';
}
I am displaying the list of users in the select box. There are two types of users i-e selected users and non selected users. The values of these users are coming from the database in two arrays i-e One array contains selected users record and other array contains all users record. Now i want if the page loads the selected users record should be shown as selected in the select box and non selected users will be shown as non selected. Here is my code:
if ($selected != false ){
foreach ($selected as $select)
{}
foreach ($data as $rows) { echo $rows->id."<br />"; echo $select->id; ?>
<option value="<?php echo $rows->id; ?>" <?php if ($rows->id == $select->id) echo "selected";?>><?php echo $rows->username; ?></option>
<?php } } else{
foreach ($data as $rows) ?>
<option value="<?php echo $rows->id; ?>" <?php if ($rows->id == $select->id) ?>><?php echo $rows->username; ?></option>
<?php } ?>
The $selected object contains the selected users list and the $data object contains non selected/total number of users
If i am not wrong you have two array like
$arr1 = array(array('id'=>1,"name"=>'abc1',"user"=>'select'),array('id'=>2,"name"=>'abc2',"user"=>'select'),array('id'=>3,"name"=>'abc3',"user"=>'select'),array('id'=>4,"name"=>'abc4',"user"=>'select'),array('id'=>5,"name"=>'abc5',"user"=>'select'),array('id'=>6,"name"=>'abc6',"user"=>'select'),array('id'=>7,"name"=>'abc7',"user"=>'select'));
$arr2 = array(array('id'=>8,"name"=>'abc8',"user"=>'noselect'),array('id'=>9,"name"=>'abc9',"user"=>'noselect'),array('id'=>10,"name"=>'abc10',"user"=>'noselect'),array('id'=>11,"name"=>'abc11',"user"=>'noselect'),array('id'=>12,"name"=>'abc12',"user"=>'noselect'),array('id'=>13,"name"=>'abc13',"user"=>'noselect'),array('id'=>14,"name"=>'abc14',"user"=>'noselect'));
$arraymerege = array_merge($arr1,$arr2); // Merge the array into one
check out the html it display the user selected. i'll just put the condition under select box if user is noselect then it show selected. Please check the image for loop
you are closing your first foreach before you actually get inside the loop...try this. (rewritten with a little shortcode added)
<select name="users" type="multiple">
<?php
//no need to write $selected == false,
//this is the same, a ! will compare this var to false
//also i use : instead because its much nicer to read i.m.o
//its also a little easier to tell the difference from a closing
//if and foreach this way if you have a lot of nested comparisons.
if (!$selected) :
//you want to loop through all rows first.
foreach ($data as $rows) :
//now look at each selected user per user
foreach ($selected as $select) : ?>
//here is where you make the comparison. no need for any other loops
//this is called a turnary comparison. its basically short code for
//if(blah) else this;
//read up on it here:
// http://www.php.net/manual/en/language.operators.comparison.php
//the <?= is the same as <?php echo. This does require for you to have
//short codes turned on in your main php.ini. It usually is though.
<option value="<?= $rows->id; ?>
<?= (($rows->id == $select->id) ? 'selected' :''); ?>
<?= $rows->username; ?>
</option>
endforeach;
endif;
?>
</select>
This is all the code you need. You should only require two loops. The very first loop would iterate through each user, and the nested loop would then compare each of those users to each selected user. Also, the ability to write cleaner code requires planning. I would suggest investing in a white board or a notepad to draw out flow charts, diagrams etc before you start writing your code. You will get a basic picture in your head. There are always 100 million ways to do one thing, but only one way that is right for you. And its up to you to find that way.
From how I read the question, he is showing all users and highlighting the selected ones in a multiple select box. He has multiple selected users so you couldn't merge the arrays together. You would have to compare each user in the table to each selected user then decide if you need to highlight that user or not.
I'm tasked with fixing a job information page at work. When someone clicks "edit job" to edit a job, a form of all the database titles and values is listed and they can click on an input box and change it and update. Basic stuff.
Now, in the input box for "Payment terms:" the percentages are written exactly like this, "40/40/20" and so in the database under payment they're listed as 40/40/20.
So the label + input box looks like this, Payment terms: [40/40/20].
<p><label for='payment'>Payment Terms</label><input name='payment'
id='payment' value='<?php if(isset($data2['payment'])) echo
$data2['payment']; ?>' /></p>
I want to change this to 4 drop down boxes where they can pick the percentages, for instance
Payment terms: [40]
[40]
[20]
[empty]
There is a TON of jobs with their own payment terms in the db and I think I may have to add 4 columns to the db instead of just 1 column named payment and values listed as %/%/%/%.
Obviously that doesn't seem like a good solution as it would take forever to fix all the jobs and enter their data for each term into the new columns.
What should I do ?
You can use explode to split the string from your database. Then you can make 4 <select> boxes:
<?php
$selects = 4;
if (isset($data2['payment'])) {
$percentages = explode("/", $data2['payment']);
// Make sure that the array is long enough by adding zeroes to the end
while (count($percentages) < $selects) $percentages[] = 0;
}
else {
$percentages = array_fill(0, $selects, 0);
}
$options = range(10, 100, 10);
for ($i = 0; $i < $selects; ++$i) { ?>
<select>
<option></option>
<?php foreach ($options as $o) { ?>
<option value="<?php echo $i; ?>"
<?php if ($o == $percentages[$i]) echo ' selected="selected"'; ?>>
<?php echo $o; ?>
</option>
<?php } ?>
</select>
<?php } ?>
I put the blank option first as it will be selected by default that way.
I have a html form with 5 rows and 30 fields. Note the attached image.
I have to capture all of these fields in an array to later convert to a csv file. Each field is labelled at follows: qt1, fa1, en1, et1, ba1, qt2, fa2, etc... Instead of taking each row and adding them to an array in php one at a time is there an easy(ier) way to accomplish this?
I can easily take each row and add them to an array the issue with this i feel would be the speed of the script and the fact that i will have to write 30 lines of array data in the php script.
Is this what you are looking for?
<?php
for($i = 0; $i < 30; $i++): ?>
<input name="qt[<?php echo $i; ?>]"/>
<input name="fa[<?php echo $i; ?>]"/>
<input name="en[<?php echo $i; ?>]"/>
<input name="et[<?php echo $i; ?>]"/>
<input name="ba[<?php echo $i; ?>]"/>
<?php
endfor;
?>
On the server side, you will receive all this data in your $_POST variable anyways, so it will all be in an array regardless. This has an advantage of saving all your values of the same row with the same index and all the same data types under one array.
Not sure about performance, but code readability and maintenance is alot easier like this.
Name the input fields like input[i][j] and you can access them later in php like: $_REQUEST['input'][i][j] as a multidimensional array.
I have created and html form which have a drop down list.
This drop down list is populated from database.
<select name="classes">
<?php
foreach() {
?>
<option value="<?php echo $id ?>"><?php echo $name ?></option>
<?php
}
?>
</select>
Now I want to get the $id and $name both. How will I do this?
I have tried this
echo $_POST['classes'];
But it only displays the $id of the select item. And I want $id and $name both.
You can't. One possibility would be placing both infos inside the value attribute, and then separating them back again with php (by using a delimiter):
<option value="<?php echo $id.'|'.$name; ?>"><?php echo $name ?></option>
In PHP:
$datas = explode('|',$_POST['classes']);
$id = $datas[0];
$name = $datas[1];
But that's not how the system is meant to be. Usually the $name would be used only as a "friendly" info for the user, cause the value might sometimes just be an INT and user won't understand what that int refers to, so we give him a word description in order to choose an option: but what you would only care of is that value indeed, which you can always use to get again the description that comes along with it (by a search to the database, for ex.)
As far as I know, when you submit that form, only the value is going to be carried over. If there is no value then the inner HTML becomes the value.
What I'd do is:
<select name="classes">
<?php
foreach($classes as $id => $name) //i'm guessing here, is this what you meant?
{ ?>
<option value="<?php echo $id.'|'.$name ?>"><?php echo $name ?></option>
<?php } ?>
</select>
In case you are not familiar, the period is the concatenation operator. Think of it as glue for pieces of a string. So I'm gluing $id to the left of "pipe" and then gluing $name onto the end of that.
Then in your handler, use split or explode to separate the two values on the pipe.
Actually, I'd do it a little different, echoing more and going in and out of php/html less, but I tried to leave your code intact as much as possible.
append name to Id and pass it as value,,and explode name from the other end
You want is and name both so while storing option value ,
put like this
<option value="<?php echo $id."_".$name;?>"><?php echo $name?></option>
and on posting data just explode the value you will get both is and name
Sending form means only sending 'value' for select field (treat 'name' in option as a label only). You can simply fetch 'name' from database when you have 'id' while handling form submission.