Use mysql query result in more than one place in code - php

I'm building a system that tracks contact lenses. I'm storing the contact lens info in a database as sometimes prices/availabilities change and i access this info from multiple points in the program. I'm trying to interface with this list using a dropdown by doing "SELECT * FROM contacts" as a query. my code looks like this :
$contact_list = mysqli_query($link, "SELECT brand FROM contacts ORDER BY brand");
Then I echo that list out in a while loop using PHP to populate the options in the dropdown.
My question is this: I have these dropdowns for each eye on the same form. So it's "Brand Right Eye"....other miscellaneous info about the right eye....then "Brand Left Eye". But ONLY the right eye is populating with the brand info because it appears first in the code. What i'm having to do is copy/paste the exact same query and do
$contact_list2 = mysqli_query($link, "SELECT brand FROM contacts ORDER BY brand");
then later if I need the dropdown again, I need to do $contact_list3..and so on. Why can i not generate a drop down using the same variable? Why does it stop responding to calling the variable after the first execution of it and is there any work around that I can implement that would allow me to not have to copy/paste the same query with a different variable association each time?
just for refernce, my php while code is this:
<select class="form-control" name = "brandOS">
<option value="0">Please Select</option>
<?php
while($row = mysqli_fetch_array($contact_list))
{
?>
<option value = "<?php echo($row['brand'])?>" name = "brandOS">
<?php echo($row['brand']) ?>
</option>
<?php
}
?>
</select>
I have this loop copy/pasted for right eye and left eye. But it only works on which ever drop down appears first in the code.

A possible solution will be more efficient in term of performance could be :
<?php
$left_eye = '<option value="0">Please Select</option>';
$rigth_eye = '<option value="0">Please Select</option>';
while($row = mysqli_fetch_array($contact_list))
{
//logic for left eye
$left_eye .= <<<HTML
<option value ="{$row['brand']}" name = "brandOS">
{$row['brand']}
</option>
HTML;
//logic for rigth eye
$rigth_eye .= <<<HTML
<option value ="{$row['brand']}" name = "brandOS">
{$row['brand']}
</option>
HTML;
}
?>
<select class="form-control" name = "brandOS">
<?php echo $left_eye ; ?>
</select>
<select class="form-control" name = "brandOS">
<?php echo $rigth_eye ; ?>
</select>
With this solution you get your result in the same while loop. If the left and right select are the same you can use the same variable.

Store the brands in an array, then you can just loop through the array.
<?php
$contact_list = mysqli_query($link, "SELECT brand FROM contacts ORDER BY brand");
$brands = array();
while($row = mysqli_fetch_array($contact_list))
{
array_push($brands, $row['brand']);
}
?>
<select class="form-control" name = "brandOS">
<option value="0">Please Select</option>
<?php
foreach($brands as $brand){
?>
<option value = "<?php echo($brand[0])?>" name = "brandOS">
<?php echo($brand[0]) ?>
</option>
<?php
}
?>
</select>

You can use a PHP array, like the SESSION one, to store values and use them across your site. Be sure you call "session_start()" method on each page you use that array, though.
//Initialize sessions
session_start();
...
//Right after getting result from query
$_SESSION['contact_list'] = $contact_list;
To use it, just be sure to call the method I told you above, and call the variable with the same syntax:
<?php
while($row = mysqli_fetch_array($_SESSION['contact_list'])) { ?>
Hope this helps.

Related

SET selected in the select tag

<select class="form-control" name="Church" id="Church">
<option>Church List</option>
<?php
$select_church = mysql_query("SELECT * FROM tblchurchs");
while($get_detail = mysql_fetch_array($select_church)){
echo'<option value="'.$get_detail['AChurchID'].'">'.$get_detail['ChurchName']." - ".$get_detail['Address'].'</option>';
}
?>
</select>
This is Edit.php
This is the result of that code
but i want is. after getting the data from the database. it will automatically selected the value of the option base on the database data.
Suppose the query result returned from tblchurchs contains a field Selected which is either set to 1 or 0. If it is set to 1 then you have to select that option in your drop-down list, and if it is set to 0, it is not selected.
<select class="form-control" name="Church" id="Church">
<option>Church List</option>
<?php
$select_church = mysql_query("SELECT * FROM tblchurchs");
while($get_detail = mysql_fetch_array($select_church)){
echo'<option value="'.$get_detail['AChurchID'].'"';
if($get_detail['Selected'] == 1)
{
echo ' selected="selected"';
}
echo '>'.$get_detail['ChurchName']." - ".$get_detail['Address'].'</option>';
}
?>
</select>
to getting selected for selected option.
first you have to save the selected data getting from database to a variable and then using an if loop you can do this.
<?php
$AChurchID=1; // this is your already selected value that is in db .
?>
<select class="form-control" name="Church" id="Church">
<option>Church List</option>
<?php
$select_church = mysql_query("SELECT * FROM tblchurchs");
while($get_detail = mysql_fetch_array($select_church)){
$sel="";
if($AChurchID==$get_detail['AChurchID']){
$sel="selected";
}
echo'<option value="'.$get_detail['AChurchID'].'" '.$sel.'>'.$get_detail['ChurchName']." - ".$get_detail['Address'].'</option>';
}
?>
This will work in your case. This is works fine in ma case. Please have a try.

How can i make Autocomplete similar to Dropdown?

I have made a autocomplete control as given below.
var PrimaryPhy = <?php include('getdata.php'); ?>;
$("#PatientID").autocomplete({
source: PrimaryPhy,
autoFocus:true
});
getdata.php
<?php
$con = mysql_connect("localhost","***","***");
if(!$con){
die("Error : ".mysql_error());
}
mysql_select_db("DBname",$con);
$patient_info = array();
$result = mysql_query("select * from tablename");
while($row = mysql_fetch_array($result)){
$patient_info[] = $row['Name'];
}
echo json_encode($patient_info);
?>
Here I have brought all the Name information of all records from the table. But i wanted to do like Dropdown with option values.
For example :
<option value="1">Item 1</option>
Likewise i need to bring the primary key ID of the table as a value for that item. How can i achieve that?
And also If i place one more autocomplete control next to this control, How can i make that first autocomplete's output as a input to the second autocomplete's input?
You can accomplish it by <datalist>.
The <datalist> element, a new addition in the HTML5 specification, allows developers to create native autocomplete dropdowns for their web applications.
The <datalist> element is used to specify all of the possible values for the autocomplete list like below:
<datalist id="languages">
<option value="HTML">
<option value="CSS">
<option value="JavaScript">
</datalist>
For detailed example, you can check here

Select list box display double same value

i have a list box that contain name of persons. In the column "Requestor" there are many names that repeat with the same name. So when I make the list box, the name shows all the name and some name shows more than one with the same name. How can i make the name just come out one only. Below are my codes.
<select name="requestor" id="requestor">
<option value="0">-- Select requestor --</option>
<?php
$getallCapex_transaction = mysql_query("SELECT * FROM capex_transaction ");
while($viewallCapex_transaction = mysql_fetch_array($getallCapex_transaction))
{
?>
<option id="<?php echo $viewallCapex_transaction ['Project_id'];?>"><?php
echo $viewallCapex_transaction['Requestor']; ?></option>
<?php } ?>
</select>
And one more question how can I show list of month and year that connect with mySql? can someone show me some codes. Thanks
Well, if there are multiple rows with the same "Requestor" but different "Project_id", that's just how it's gonna turn out. You can select distinct by Requestor in your SQL, but that will leave you unaware of what record you're pointing at.
Put your values in an array and some something like array_unique to create an unique array. Try something like this:
<?php
$requestorArray = array();
?>
<select name="requestor" id="requestor">
<option value="0">-- Select requestor --</option>
<?php
$getallCapex_transaction = mysql_query("SELECT * FROM capex_transaction ");
while($viewallCapex_transaction = mysql_fetch_array($getallCapex_transaction))
{
$requestorArray[$viewallCapex_transaction ['Project_id']] = $viewallCapex_transaction['Requestor'];
}
$requestorArray = array_unique($requestorArray);
foreach($requestorArray as $projectId => $requestor)
?>
<option id="<?php echo $projectId;?>"><?php
echo $requestor; ?></option>
<?php } ?>
</select>

echo drop down menu onto a page

I have a drop down menu with two options in html, I also created a PHP script that checks what option from the drop down menu has been selected and based on the selection executes a mysql query to fetch data from database.
But I am also trying to echo out a new drop down menu with the results obtained from database and that is where the I am struggling because no errors are diaplayed but also no drop down menu is 'echoed' out onto the page.
HTML:
<?php require "course.php" ?>
<select id="workshop" name="workshop" onchange="return test();">
<option value="">Please select a Workshop</option>
<option value="Forex">Forex</option>
<option value="BinaryOptions">Binary Options</option>
</select>
PHP code:
$form['workshop'] = $_POST['workshop'];
$form['forex'] = $_POST['Forex'];
$form['binary'] = $_POST['Binary'];
//Retrieve Binary Workshops
if($form['workshop'] == 'Forex'){
$sql2 = "SELECT id, course, location FROM courses WHERE course LIKE '%Forex%' OR course LIKE '&forex%'";
$query2 = mysqli_query($link, $sql2);
echo "<select id='Forex' name='Forex' style='display: none'>";
while($result2 = mysqli_fetch_assoc($query2)){
echo "<option value=''>".$result2['course']."</option>";
}
echo "</select>";
echo '</br>';
}
Could someone point out a mistake I am doing or perhaps suggest where I could look for answers
you said $query2 is displaying value in print_r.so the only mistake i find in your code is display:none .
remove display:none
echo "<select id='Forex' name='Forex' style='display: none'>";
----------------------------------------------------------------------^

Prepare drop down list and use multiple times

I've got a database of regions for sale and I'm displaying it in a table for admins to edit the cost, whether it's for sale and who's selling it (seller). For the seller I've got a drop down list with an first option as the current owner for ease of use and then under that I just want it to list the rest of the possible users that could be sellers.
Thing is, there's a good 200 regions and I don't want to loop through every user for each region to display them in a list. Is there a way I can prepare the many 's rather than loop every time. Something like preparing the list as a string then inserting it as HTML? Wouldn't know how to do it that way if possibe.
Thanks in advance.[
let's say you make your list this way:
$str = "<select>";
$result = mysql_query("select * from sellers");
while($row=mysql_fetch_assoc($result)){
$str.= '<option value="'.$row['id'].'">'.$row['name'].'</option>';
}
$str.="";
And you just output that everywhere you want the list to appear.
Also, you don't have to put the one you want selected by default at the top, you can also add the word 'selected' to the option tag like so:
<option value='1' selected>John Doe</option>
If I understand the question correctly. You need to create an array of option tags.
$Name = array();
$q = "SELECT name FROM users";
$r = mysqli_query ($dbc, $q) or die("Error: ".mysqli_error($dbc));
while($row = mysqli_fetch_array($r)){
$Name[] = "<option value='$row[\"name\"]'>$row[\"name\"]</option>";
}
Then anytime you need that you just loop through array like so:
<select id='thisselect' name='thisselect'>
<?php
foreach($Name as $key =>$line){
echo "$line";
}
?>
</select>
All the answers are good but I suggest you to separate the logic from the content itself, instead of doing all that with concatenated strings, you should do it like this:
<select id="region" name="region">
<?php foreach($regions as $key => $region) ?>
<option value="<?php echo $key ?>"><?php echo $region ?></option>
<?php endforeach; ?>
</select>

Categories