Solved

Display Specific Column Of Drop Down Box

Posted on 2013-01-28
11
395 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
 

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
<? versus <?php 5 37
Convert html page to a php post email form automatically? 10 33
php construct 5 13
category table 2 8
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…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

867 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

21 Experts available now in Live!

Get 1:1 Help Now