Create special dynamic select - php

I want to do the next thing but I don't know how to do this, I'll try to explain me
I have an field generated by php code like this (Works)
<select id="profiles_select" name="profiles_select">
<?php
do {
?>
<option value="<?php echo strtoupper($system['profile']);?>">
<?php echo strtoupper($system['profile']);?></option>
<?php
} while($system = mysql_fetch_assoc($r)); //the "$r" it's the query
$rows = mysql_num_rows($r);
if($rows > 0) {
mysql_data_seek($r, 0);
$systemas = mysql_fetch_assoc($r);
}
?>
</select>
The query
<?php
$q="SELECT DISTINCT profile FROM sys_profiles";
$r=mysql_query($q,$ConecLocal) or die(mysql_error());;
$systemas=mysql_fetch_assoc($r);
$tsys=mysql_num_rows($r);
?>
What I need?
I need generate another similar to first generated by php code but, this time I need made a Query including the value of the first , something like this:
<?php
$value_select=$_GET['profiles_select'];
$q2="SELECT DISTINCT systems FROM sys_profiles where profile=$value_select";
$r2=mysql_query($q,$ConecLocal) or die(mysql_error());;
$profiles2=mysql_fetch_assoc($r);
$tsys=mysql_num_rows($r);
?>
Next of the query I need show in the another the query result, something similar to the first select (generated by php), but do the query when the first of the it's selected
<select id="systems_select" name="system_select">
<?php
do {
?>
<option value="<?php echo strtoupper($system['systems']);?>">
<?php echo strtoupper($profiles2['systems']);?></option>
<?php
} while($profiles2 = mysql_fetch_assoc($r2)); //the "$r2" it's the another query
$rows2 = mysql_num_rows($r2);
if($rows2 > 0) {
mysql_data_seek($r2, 0);
$systemas = mysql_fetch_assoc($r2);
}
?>
</select>
Thanks for the help.

Related

Sorting a PHP array fetched through a while loop

I am populating a select option using a php while loop from a mysql table. The following is the php code:
<?php
session_start();
$database = $_POST['folder'];
include('connect.php');
$connect = mysqli_connect($hostname, $username, $password,$database );
if ($connect->connect_error) {
die("Connection failed: " . $connect->connect_error);
}
$result = $connect->query("select name from treeview where parent_id = 1;");
?>
<option value="">Select Location</option>
<?php while($row1 = mysqli_fetch_array($result)):;?>
<option value="<?php echo $row1['name'];?>"><?php echo $row1['name'];?></option>
<?php endwhile;?>
This works correctly, except for the fact that the fetched items are not sorted alphabetically. I understand this could be trivial, but I couldn't get it right. I tried the following:
Option 1
<?php while($row1 = mysqli_fetch_array($result)):;
$row1 = sort($row1);
?>
<option value="<?php echo $row1['name'];?>"><?php echo $row1['name'];?></option>
Option 2:
<?php while($row1 = mysqli_fetch_array($result)):;?>
<option value="<?php echo sort($row1['name']);?>"><?php echo sort($row1['name']);?></option>
Both didn't work.
You need to change your SQL query and use ORDER BY
See here link
You are attempting to sort a single row, instead of between the rows. Sorting all the rows will require you to fetch them all of them and then sort them. An easier approach, IMHO, would be to sort them in the query itself:
$result = $connect->query("select name from treeview where parent_id = 1 ORDER BY 1;");
# Here ------------------------------------------------------------------^

Use mysql query result in more than one place in code

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.

How create a drop down list box using data from MySQL table

I need some help in populating a drop down down list from a mysql table. I'm new to php and I am having a hard time. Here's my code and I know it's wrong, I just don't know where.
My current output is just a drop down box with nothing inside it.
My expected output is that it would show the driver's name from the mysql table.
<?php
$Hehe = $mydb->getALL('SELECT drivername FROM driver;'); //select from all users
?>
<select name=mydriver value=''>Driver Name</option> // list box select command
<?php
foreach($Hehe as $hehe){//Array or records stored in $row
?>
<option name = "mydriver"><?phpecho $Hehe['drivername']?></option>
</select>
<?php
}
?>
the getALL function:
function getAll($query) {
$result = $this->conn->prepare($query);
$ret = $result->execute();
if (!$ret) {
echo 'PDO::errorInfo():';
echo '<br />';
echo 'error SQL: '.$query;
die();
}
$result->setFetchMode(PDO::FETCH_ASSOC);
$reponse = $result->fetchAll();
return $reponse;
}
You're using wrong array to get the drivername. It should be $hehe['drivername'], not $Hehe['drivername']. Like I said, use some meaningful variable names in your code, it would be easy for you track down the error. Also </select> should be outside of foreach loop.
<?php
$Hehe = $mydb->getALL('SELECT drivername FROM driver;'); //select from all users
?>
<select name="mydriver">Driver Name</option> // list box select command
<?php
foreach($Hehe as $hehe){//Array or records stored in $row
?>
<option value="<?php echo $hehe['drivername']; ?>"><?php echo $hehe['drivername']; ?></option>
<?php
}
?>
</select>
Sidenote: Always turn on error reporting, add these two statements ini_set('display_errors', 1); error_reporting(E_ALL);at the very top of your PHP scripts to debug any syntax related issues.

How can I create <select> optgroups in html using data from mysql?

SO I have about 60 fields of data queried from a database. They look like this in page source:
<option>Genesis</option>
<option>Exodus</option>
<option>Leviticus</option>
For example I wanna have it so that 1-20 are a certain optgroup and then 20-60 is another. Could I do it using my format of options of would they have to be numbered like this:
<option value="1">Genesis</option>
<option value="2">Exodus</option>
<option value="3">Leviticus</option>
This is for my php class, but i dont think php is involved in making optgroups here, or is it? Thank you hope you understand my question. Hoping for help.
I pull the data from mysql using this :
//Query the database for the results we want
$query = $mysqli->query("select distinct bname as Name from kjv"); ?>
And then output it in the select dropdown box using this:
<select>
<?php while($option = $query->fetch_object()){ ?>
<option><?php echo $option->Name; ?></option>
<?php } ?>
</select>
Use:
<select>
<?php
$i=1;
while($option = $query->fetch_object()){
if($i%10==1) echo "<optgroup label='Option Group'>";
echo "<option value='$i'>".$option->Name."</option>";
$i++;
if($i%10==1) echo "</optgroup>";
}
?>
</select>
Using while loop :
$query = "SELECT * FROM example";
$result = mysql_query($query) or die(mysql_error());
foreach ($row = mysql_fetch_array($result)) {
echo "'<option value={$row['id']}>{$row['value']}</option>'";
}

selecting ALL values in PHP Dropdown

I have dropdown list that gets values form array based on a mySQL SELECT query. Everything is working fine except that I would like to add the option to select ALL values in the list. Here is my code...
$dataArray = array();
$result = mysql_query("SELECT id, user_name FROM apsc_customers");
while($row = mysql_fetch_assoc($result)) {
$dataArray[$row['id']] = $row['user_name'];
}
AND
if($this->customer_id == ""){
$this->arrFilteringFields[_CUSTOMER] = array("table"=>DB_PREFIX."customers", "field"=>"id", "type"=>"dropdownlist", "source"=>$dataArray, "sign"=>"like%", "width"=>"");
}
Looking forward to any replies.
Thx
Where is your dropdown list? I can't see it in your code. Do you mean html element select created by PHP and based on data from MySQL table? Then you need to use javascript code to select all options in such dropdown list. What is $this in your code? Provide more information, more code and more clarificatios.
Do you mean:
<select>
<?php
while($row = mysql_fetch_assoc($result)) {
?>
<option value="<?php echo $row['id']; ?>"><?php echo $row['user_name']; ?></option>
<?php } ?>
</select>

Categories