Solved

Display Specific Column Of Drop Down Box

Posted on 2013-01-28
11
394 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:DS928
Comment Utility
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
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:DS928
Comment Utility
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
Comment Utility
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
Comment Utility
Pointed me in the right direction.  Altogether he was a Big help!  Thank you.
0
 
LVL 35

Expert Comment

by:Robert Schutt
Comment Utility
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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
The viewer will learn how to dynamically set the form action using jQuery.
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.

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now