Solved

Display Specific Column Of Drop Down Box

Posted on 2013-01-28
11
405 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 

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 500 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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

617 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