Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Display Specific Column Of Drop Down Box

Posted on 2013-01-28
11
Medium Priority
?
409 Views
Last Modified: 2013-01-31
I have a three dropdowns on one page and I am trying to display the name field on the next page.  Right now I am just getting the first column ID.

with this
<?php
echo $Doggie ;echo $Kitty  ;echo $Pig;

Open in new window


Showing this  161313
No spaces or backslashes.

I need this  Boston\ Back Bay\ American

How do I get the "name" field to show? Not the ID field.
0
Comment
Question by:DS928
  • 7
  • 4
11 Comments
 

Author Comment

by:DS928
ID: 38828852
OK, I got the space part.  Now how do I get the name to come up?

echo $Doggie.' '.$Kitty.'  '.$Pig;

Open in new window

0
 

Author Comment

by:DS928
ID: 38829535
In MS Access it is something like this when you reference a column in a listbox.

echo $Doggie['name']'  '.$Kitty['name']'  '.$Pig['name'];

Open in new window


Doggie, Kitty and Pig being the names of the listbox and the name of the field is name.  Isn't it the same in php?
0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 38830160
You're not giving a lot of info, but I assume you have assigned these variables from a posted form. These will just correspond to the value of the dropdown on the previous page. The corresponding text is not posted normally.

I think you have 2 options:

1) do a new database query with each of the ID's to get the corresponding name.

2) if you don't need the ID's at all and the names are relatively simple then you could just stop using values in the dropdowns (or duplicate the name in the value) and the names will be posted to this page.

You should really post a little more detail of your pages (not the whole shebang, just enough to replicate the problem).

For example:
ShowForm.php:
<html>
<body>
<form action='ProcessForm.php' method='POST'>
<select name='Doggie'><option>-- Please select</option><option>Boston</option></select>
<input type='submit'>
</form>
</body>
</html>

Open in new window

ProcessForm.php:
<?php
error_reporting(E_ALL);

$Doggie = $_POST['Doggie'];
?><html>
<body>
Doggie = <?= $Doggie ?>
</body>
</html>

Open in new window

0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 

Author Comment

by:DS928
ID: 38830688
How about if I have this seperate php file.  progress.php and use a function that returns the results of the php file?  But how would I write that function that calls progresss.php and displays the name field in the three listboxes.  $Doggie $Kitty and $Pig give me the ID field.  Here is the progress php file.

<?php
require('config.php');

$sql = mysql_query("SELECT tblLocations.CityID, tblLocations.AreaID, tblLocations.CuisineID, tblCities.CityName, tblAreas.AreaName, tblCuisines.CuisineName
				FROM tblCuisines INNER JOIN (tblAreas INNER JOIN (tblCities INNER JOIN tblLocations ON tblCities.CityID = tblLocations.CityID)
				ON tblAreas.AreaID = tblLocations.AreaID) ON tblCuisines.CuisineID = tblLocations.CuisineID
				GROUP BY tblLocations.CityID, tblLocations.AreaID, tblLocations.CuisineID, tblCities.CityName, tblAreas.AreaName, tblCuisines.CuisineName
				HAVING tblLocations.CityID='$Doggie' AND tblLocations.AreaID='$Kitty' AND tblLocations.CuisineID='$Pig'");
				echo '<option value="'.$row['Rid'].'">'.$row['name'].'</option>';
; 

mysql_close();
?>

Open in new window


So I tried this, just to check my theory, and yes indeed it does return.

<?php
function square($num)
{
    return $num * $num;
}
echo square(4);   // outputs '16'.

?> 

Open in new window


So f I do a CONCAT in my query to refelct the three fields that I need.  CityName, AreaName and CuisineName.  How would I get the function to return these from the php file or is it in the function that the query is put.  As you guessed I am new to this!  Thank you for your help!
0
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 2000 total points
ID: 38831060
That could work, but your code is skipping a few steps. I think we need to go step by step, first try:
<?php
require('config.php');

$sqlQ = "SELECT ... tblLocations.CityID='$Doggie' AND tblLocations.AreaID='$Kitty' AND tblLocations.CuisineID='$Pig'";
echo $sqlQ;

mysql_close();
?>

Open in new window

and see if that return that partial query text to your page.

If that works, we may need to add query parameters to the url you call. Can you show that part of the code please?
0
 

Author Comment

by:DS928
ID: 38831714
OK, Thats part of the problem.  I don't know how to call a php file with a function. Thank you.

I tried this.

<?php
function GetProgress($CityName){
  require("config.php");
  $result = mysql_query("SELECT tblCities.CityName FROM `tblCities` WHERE `CityID` = '$Doggie' ") or trigger_error(mysql_error()); 
  return $result;
  echo $result;
 }
?> 

Open in new window


and this is the results that show on the webpage of your code.

SELECT ... tblLocations.CityID='16' AND tblLocations.AreaID='131' AND tblLocations.CuisineID='3'
0
 

Author Comment

by:DS928
ID: 38831972
Eureka!  This works!

<?php

function GetAccounts($Doggie,$Kitty,$Pig){
require("config.php");
$result1 = mysql_query("SELECT tblLocations.CityID, tblLocations.AreaID, tblLocations.CuisineID, tblCities.CityName, tblAreas.AreaName, tblCuisines.CuisineName
				FROM tblCuisines INNER JOIN (tblAreas INNER JOIN (tblCities INNER JOIN tblLocations ON tblCities.CityID = tblLocations.CityID)
				ON tblAreas.AreaID = tblLocations.AreaID) ON tblCuisines.CuisineID = tblLocations.CuisineID
				GROUP BY tblLocations.CityID, tblLocations.AreaID, tblLocations.CuisineID, tblCities.CityName, tblAreas.AreaName, tblCuisines.CuisineName
				HAVING tblLocations.CityID='$Doggie' AND tblLocations.AreaID='$Kitty' AND tblLocations.CuisineID='$Pig'") or trigger_error(mysql_error()); 
return $result1;
}
$result1 = GetAccounts($Doggie,$Kitty,$Pig);
while($row = mysql_fetch_array($result1)){ 
foreach($row AS $key => $value) { $row[$key] = stripslashes($value); } 
$thecity = $row["CityName"];
$thearea = $row["AreaName"];
$thecuisine = $row["CuisineName"];
echo $thecity.'  '.$thearea.'  '.$thecuisine;

Open in new window


Question:  I have the two queries and the are almost exactly the same except for one field,  Could I not just run one and pull the info I need from the one and assign the results to where I need them on the page?  Also the second query groups the results, but I just tried it and it seems to work.  I have three results coming up but only one showed.  Thank you for your help.
0
 

Author Closing Comment

by:DS928
ID: 38832003
Pointed me in the right direction.  Altogether he was a Big help!  Thank you.
0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 38834237
Sorry I was out last night. You seem to have solved it, great! If you still want an answer to that question could you rephrase it a bit because I don't get it. The foreach does seem strange, instead of assigning, try printing out the value, although I think you already have what you need from another question.
0
 

Author Comment

by:DS928
ID: 38838169
Thank you.  It works well.  Is there a better way to do this? If so I am all ears.  Once again, thank you for your help!
0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 38838880
Is there a better way to do this?
I suspect yes, but can't be sure because of the 'standalone' nature of your posted pieces of code. I can't determine from them how they are linked together. In my first post I showed some 'minimized' test files. If you still want to get to the bottom of this, it would help if you could post something similar to that: the smallest amount of code you can post (without leaving anything essential out) to make it clear how the pieces and/or pages are interconnected and which part could be improved.
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
In this article, we’ll look at how to deploy ProxySQL.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question