473,387 Members | 1,453 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

A challenge query / results

Chrisjc
375 256MB
Okay so I think I figured out the question I need to ask one of you enlighten php guys

I am in need of a query that will get all fields opened up so duh * ha no but seriously here is the challenge

We have drop down box 1 = State
and drop down box 2 = City

User picks (California)
User picks ( Brea )
Code must capture the 2 results (res) and now only query the fields that match those 2 selections....

and display what was found

so the database looks like this

Expand|Select|Wrap|Line Numbers
  1.    Field Type Attributes Null Default Extra Action 
  2.   id int(16)  No  auto_increment               
  3.   state varchar(150)  No                 
  4.   company varchar(100)  No                 
  5.   address text  No                 
  6.   city text  No                 
  7.   stateabb varchar(100)  No                 
  8.   zip varchar(100)  No                 
  9.   phone text  No                 
  10.   web text  No                 
  11.  
  12.  
So drop down box 1 & 2 are already completed and working... value being passed via ($res = mysql_query) then the q for the drop down and then
while ($row = mysql_fetch_assoc($res)) {
$ci = $row['city'];
echo "<option value='$ci'>$ci</option>";
}
echo '</select>';

so after that I need to make the selected be passed and noted that they are seen and now find and display the rows left which are the following

[PHP]
$id = $row['id'];
$co = $row['company'];
$ad = $row['address'];
$ci = $row['city'];
$sa = $row['stateabb'];
$zi = $row['zip'];
$ph = $row['phone'];
$we = $row['web'];
$Array=array ();
}
}
echo "$co <br>";
echo "$ad <br>";
echo "$ci, $sa $zi <br>";
echo "$ph <br>";
echo "$we <br>";
echo "<br>";
}
[/PHP]

However the code I did only shows me 1 result when I know there are more to be shown

Can be sorted by address or company either is fine. Can anyone help me please?
Jan 17 '08 #1
6 1580
dlite922
1,584 Expert 1GB
Let me get this straight:

You have 2 boxes, State, and City. User chooses one of each and you query the database based on those two criteria and output all the results.

If i'm correct, you have not shown us the entire code (or even relevant code), so god knows what could be wrong with it.

I assume that you also get the State and City from DB, or do you list every state and city in the US and force the user make the match. i.e. i could choose "California" and "Denver" ?

Here's how i would write to capture the data.

first use classes.

make a $office class for example which contains the data members ( the table columns)

on your result set loop and capture each result row into an object and pass that back as such: (this is not VALID php code, more like pseudo)

[PHP]

$officeObjList = array();

while($row = $dbResult)
{
$office = new Office($row['id'],
$row['company'],
$row['address'],
$row['city'],
$row['stateabb'],
$row['zip'],
$row['phone'],
$row['web']);

array_push($officeObjList , $office);

}

return $officeObjList;



then on the display,

foreach ($officeObjList as $office)
{
echo $office->company . "<br/>" . $office->address ........etc..

}

[/PHP]

okay, maybe it is somewhat valid PHP code, i tried to cut corners.

assumes your constructor in the class sets your arguments.

if you don't know classes, not is a good time to learn, they help you big time. (think of them as really really nice associate arrays)

:D
Jan 18 '08 #2
Chrisjc
375 256MB
ahh well sense you put it that way... here is the full code starting with

"dealerlocater.php"

[PHP]
<script type="text/javascript" src="ajax/ajax.js"></script>
<script language="JavaScript" type="text/JavaScript">

var div1 = '<select id="city" name="city" style="font-family:Tahoma;font-size:10pt;width:184px;" onchange="getPart(this)"><option value="">Select Your City</option></select>';
var div6 = '<p></p>';
var div7 = '<p></p>';

var ajax = new sack();
var selState;
var selCity;


function buildtop() {
ajax.requestFile = 'getStateInfo.php?buildtop=1';
ajax.onCompletion = makeTop;
ajax.runAJAX();
}
function makeTop() {
document.getElementById("divmsg6").innerHTML = ajax.response;
}
// ================================================== ===============================
// GET the City drop down list after user has selected state
// ================================================== ===============================
function getCity(sel) {
selState = sel.options[sel.selectedIndex].value;
if(selState.length>0){
ajax.requestFile = 'getStateInfo.php?getcity=1&state='+selState;
ajax.onCompletion = createDropdownMake;
ajax.runAJAX();
}
}
// ================================================== ===============================
// DISPLAY the City drop down list
// ================================================== ===============================
function createDropdownMake() {
document.getElementById("divmsg1").innerHTML = ajax.response;
document.getElementById("divmsg7").innerHTML = div7;
}

function getPart(sel) {
selCity = sel.options[sel.selectedIndex].value;
if(selCity.length>0){
ajax.requestFile = 'getStateInfo.php?getpart=1&state='+selState+'&cit y='+selCity;
ajax.onCompletion = createPart;
ajax.runAJAX();
}
}
function createPart() {
document.getElementById("divmsg6").innerHTML = ajax.response;
document.getElementById("divmsg7").innerHTML = div7;
}
// ================================================== ===============================
// show the div6 when page has loaded for first time
// ================================================== ===============================
function showDiv6 () {
// document.getElementById("divmsg6").innerHTML = div6;
}
</script>
<?php
// ================================================== =======
// Populate the state selection list from the database
// ================================================== =======
echo '<form action="" method="post">';
echo '<select id="state" name="state" style="margin-bottom:4px;font-family: Tahoma; font-size: 10pt; height:28px;width:184px;" onchange="getCity(this)">';
echo '<option value="">Select Your State</option>';
// ================================================== =======
// Connection to the Database
// ================================================== =======
include ('db functions/db_connect.php');
// ================================================== =======
// SELECT state to make drop down list
// ================================================== =======
$res = mysql_query("SELECT state FROM dealerlocater GROUP BY state ORDER BY state")
or die("Invalid query: " . mysql_query());
while ($row = mysql_fetch_assoc($res)) {
$st = $row['state'];
echo "<option value='$st'>$st</option>";
}
echo '</select>';
?>
<DIV ID="divmsg1" STYLE="font-family:Tahoma;font-size:10pt;height:26px;overflow:hidden;">
<select id="city" name="city" style="font-family:Tahoma;font-size:10pt;width:184px;" onchange="getPart(this)"><option value="">
Select Your City</option></select>
</DIV>
<DIV ID="divmsg6">
</DIV>
<DIV ID="divmsg7">
</DIV>
[/PHP]


and now the 2nd page

"getStateInfo.php"

[PHP]
<?php
//************************************************** ***********************************
//1. drop down State - select all (grouped by)
//************************************************** ***********************************
// ================================================== =============================
// Connection to the Database
// ================================================== =============================
include ('db functions/db_connect.php');
// ================================================== ========================================
// Get City after user selected State and create City-dropdown
// ================================================== ========================================
if(isset($_GET['getcity']) AND isset($_GET['state'])){
$st = $_GET['state'];
$res = mysql_query("SELECT city FROM dealerlocater WHERE state='$st' GROUP BY city ORDER BY city")
or die("Invalid query: " . mysql_query());
echo '<select id="city" name="city" style="font-family:Tahoma;font-size:10pt;width:184px;" onchange="getPart(this)">';
echo '<option value="">Select Your City</option>';
while ($row = mysql_fetch_assoc($res)) {
$ci = $row['city'];
echo "<option value='$ci'>$ci</option>";
}
echo '</select>';
}
// ================================================== ================================================== =
// Construct parts overview after user selected state, city.
// ================================================== ================================================== =
if( (isset($_GET['getpart']) AND isset($_GET['state']) AND isset($_GET['city']))
OR (isset($_GET['search']) ) ) {
if(isset($_GET['getpart'])) {
$st = $_GET['state'];
$ci = $_GET['city'];
$res = mysql_query("SELECT * FROM dealerlocater WHERE city='$ci' LIMIT 0, 10")
or die("Invalid query: " . mysql_query());

// Loop through
for ($i=1; $i<=4; $i++) {
// build the output array sorted by category
$Array=array();
while ($row = mysql_fetch_assoc($res)) {
$id = $row['id'];
$co = $row['company'];
$ad = $row['address'];
$ci = $row['city'];
$sa = $row['stateabb'];
$zi = $row['zip'];
$ph = $row['phone'];
$we = $row['web'];
$Array=array ();
}
}
echo "$co <br>";
echo "$ad <br>";
echo "$ci, $sa $zi <br>";
echo "$ph <br>";
echo "$we <br>";
echo "<br>";
}
}
?>
[/PHP]


Now I get everything working.... however my issue is what I was asking to be made... I can not make this code here display more then one out put...

In simple terms it will only show me 1 result I search....

When the database holds more then one for the same search I.E

State = WYOMING
City = Casper
Number to display = 2
Number being displayed by the code above = 1...

And for you to see what I mean see LINK here... do that same search

So at the moment it will return this
Dan Winer
3086 Herrington Dr
Casper, WY 93445
(805) 474-1312

and it should be returning one more company underneath it....
Jan 18 '08 #3
stepterr
157 100+
Chrisjc, it looks like you are using the echo outside your while loop. Therefore it would only show the last one that was retrieved from the query. Try placing the echo statements in your while loop.
Jan 20 '08 #4
Chrisjc
375 256MB
Chrisjc, it looks like you are using the echo outside your while loop. Therefore it would only show the last one that was retrieved from the query. Try placing the echo statements in your while loop.

So move the two } down under the echos?
Jan 20 '08 #5
Chrisjc
375 256MB
ha!!!!!!!!!!!!!!! after two damn days....

Thank you for catching that!!! life saver!

also something small and dumb... Like my teachers always said its always better to have 2 sets of eyes over look a code with an issue then it is one set....


Thanks again!


Issue Solved
Jan 20 '08 #6
stepterr
157 100+
ha!!!!!!!!!!!!!!! after two damn days....

Thank you for catching that!!! life saver!

also something small and dumb... Like my teachers always said its always better to have 2 sets of eyes over look a code with an issue then it is one set....


Thanks again!


Issue Solved

You're welcome! Glad that solved it. :-)
Jan 20 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Random | last post by:
Trying to solicit some quick advice on my procedure architecture so I don't have to waste too much time with trying different objects. I'm still unfamiliar with a lot of the new XML...
6
by: redeck | last post by:
I have table A with 2 columns: PK and Txt , size - 1000 rows. Also, I have table B with 100000 rows and 100 integer columns, each one is a foreign key to A's PK. I need to create a view in which...
78
by: wkehowski | last post by:
The python code below generates a cartesian product subject to any logical combination of wildcard exclusions. For example, suppose I want to generate a cartesian product S^n, n>=3, of that...
19
by: octangle | last post by:
This code is attempting to find records that have a RegJrnID that does not occur more than one time in the table. The reason that I want to find records with non-duplicated RegJrnID values is to...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.