Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 411
  • Last Modified:

Display Specific Column Of Drop Down Box

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
DS928
Asked:
DS928
  • 7
  • 4
1 Solution
 
DS928Author Commented:
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
 
DS928Author Commented:
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
 
Robert SchuttSoftware EngineerCommented:
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
DS928Author Commented:
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
 
Robert SchuttSoftware EngineerCommented:
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
 
DS928Author Commented:
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
 
DS928Author Commented:
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
 
DS928Author Commented:
Pointed me in the right direction.  Altogether he was a Big help!  Thank you.
0
 
Robert SchuttSoftware EngineerCommented:
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
 
DS928Author Commented:
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
 
Robert SchuttSoftware EngineerCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 7
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now