Forum Moderators: coopster
I did have a quick question just for my knowledge as to what these lines of codes do?
$pbrand = '';
$pmodel = '';
Now that i got everything working how i want according to my database, my next step is to get the results i want to populate my table according to the model that is selected. I tried copying code and changing to the corresponding DB fields that i wanted, but it doesnt seem to be working. Can i get some help figuring out what i need to do to get this working in the following format? Thanks
Here is my table:
<?php // Appears once model is selected
if(isset($_POST['Submit'])) {?>
<p class="headline">Your Results for <? echo $pbrand;?> <? echo $pmodel;?></p>
<table width="100%" border="0" cellpadding="1">
<tr>
<td bgcolor="#CCCCCC"><table width="100%" border="0" align="center" cellpadding="2" cellspacing="2" class="TableContent">
<tr>
<td width="33%" class="TableHeader">Image</td>
<td width="22%" class="TableHeader">Tip Item # </td>
<td width="17%" class="TableHeader">CLA # </td>
<td width="28%" class="TableHeader">Bluetooth Capable? </td>
</tr>
<tr>
<td rowspan="2" bgcolor="#FFFFFF" valign="top" onmouseover="this.style.cursor='pointer'" onmouseout="this.style.cursor='default'" onclick="MM_openBrWindow('<? echo $pimage2;?>','Image','location=yes,scrollbars=yes,resizable=yes,width=600,height=600')"><img src="<?php echo $pimage;?>" /></td>
<td align="center" bgcolor="#FFFFFF"><?php echo $pitem;?></td>
<td align="center" bgcolor="#FFFFFF"><?php echo $pcla;?></td>
<td align="center" bgcolor="#FFFFFF"><?php echo $pbt;?></td>
</tr>
<tr>
<td colspan="3" align="center" bgcolor="#FFFFFF"> </td>
</tr>
</table></td>
</tr>
</table>
<?php }?>
And here is are the changes i made to the code:($image, and $image2 reference thumbnail and large images called from database)
$pbrand = '';
$pmodel = '';
$pimage = '';
$pimage2 = '';
$pitem = '';
$pcla = '';
$pbt = '';
and
if(isset($_POST['Submit']))
if(in_array($_POST['model'],$rows))
$pmodel = $_POST['model'];
$pitem = $_POST['item'];
$pcla = $_POST['CLA'];
$pbt = $_POST['bluetooth'];
$pimage = $_POST['image'];
$pimage2 = $_POST['image2'];
So when they're not assigned, when the value is checked further down to determine whether or not to display the model box and table, an error would be genertated if they weren't defaulted to nothing.
On that bottom section where you're assigning your other variables from POST data, those will error before the form is posted. Put a { right after $_POST['Submit'])) and a closing one after the last line of your thread post.
I did something like this. Am i close or way off?
$qry = mysql_query("SELECT model FROM mt_tips WHERE brand='$pbrand'",$mysql) or die(mysql_error());
$pimage = mysql_query("SELECT image FROM mt_tips WHERE model='$pmodel'",$mysql) or die(mysql_error());
$pimage2 = mysql_query("SELECT image2 FROM mt_tips WHERE model='$pmodel'",$mysql) or die(mysql_error());
$pitem = mysql_query("SELECT item FROM mt_tips WHERE model='$pmodel'",$mysql) or die(mysql_error());
$pcla = mysql_query("SELECT cla FROM mt_tips WHERE model='$pmodel'",$mysql) or die(mysql_error());
$pbt = mysql_query("SELECT bluetooth FROM mt_tips WHERE model='$pmodel'",$mysql) or die(mysql_error());
An example:
$qry = mysql_query("SELECT model FROM mt_tips WHERE brand='".[url=http://us2.php.net/mysql_real_escape_string]mysql_real_escape_string[/url]($pbrand)."'",$mysql) or die(mysql_error());
$row = mysql_fetch_array($qry); //assuming only one row is returned
echo '<pre>';
print_r($row);
echo '</pre>';
Also, do not forget to escape your data!
mysql_real_escape_string, as I have demonstrated above :)
Since the script is getting long and we're seeing snippets, it's possible that where you're doing the query, $pbrand and $pmodel aren't assigned to the form values yet. Before the queries, do this, possibly where they're set to nothing (unless that's below the queries). Make sure your database connection is already open, though:
if(isset($_POST['pbrand']))
$pbrand = mysql_real_escape_string($_POST['pbrand']);
else
$pbrand = '';
if(isset($_POST['Submit']))
$pmodel = mysql_real_escape_string($_POST['pmodel']);
else
$pmodel = '';
Those lines get the values from the form if they exist, and set them to empty strings if not. In the one that sets pmodel we check for the submit button instead of the list box because, the way the form is designed to display pmodel can exist before it's really valid.
Simplifying those queries a bit might help:
$qryresource = mysql_query("SELECT * FROM mt_tips WHERE model='$pmodel') or die(mysql_error());
$dat = mysql_fetch_array($qryresource);
$pimage = $dat['image'];
$pimage2 = $dat['image2'];
$pitem = $dat['item'];
$pcla = $dat['cla'];
$pbt = $dat['bluetooth'];
Do you see what's going on? When you ask mysql a question, it gives you back the resource identifier for where you can get the answer (we're storing it in $qryresource here), but you still have to go get the answer. That's what mysql_fetch_array does for you - it gives you the answer for the question identifed by $qryresource. The answer gets stored into $dat as an array, then we get each of the variables by assigning them to the elements of the array. Instead of getting one field at a time, the '*' in the query returns all fields for the record.
Any clearer? (As mud, right? <grin>)
Yes it was. I want the $pimage, pimage2, pitem, pcla, and pbt to be determined by the selected MODEL on submit, not by the brand.
I tried a copy/paste of your code, and now im not getting field 2(models) to populate. I am still playing with it, and reading the other thread provided to see what i can learn.
One question. In this part of the code, the 'pbrand' inside POST, should that be as is, or should it be the name of the mysql field? my field's name is just "brand".
if(isset($_POST['pbrand']))
$pbrand = mysql_real_escape_string($_POST['pbrand']);
else
$pbrand = '';
Thanks!
Because in the <form> in the html, the form field is named 'pbrand', we refer to it in the php code as $_POST['pbrand']. When you ask mysql any questions you refer to the field name the way you set it in the table, and you get the two coordinated like this: "... WHERE brand='$pbrand'" or by referring to it directly: "... WHERE brand='" . $_POST['pbrand'] . "'"
Many programmers, (I included) will name their form fields the same name as the table fields because that way you can build forms 'on the fly' by reading table information. In this case I prepended a 'p' to your form fields to keep it clear in your mind that it came from the POST and that the two aren't necessarily the same value all the time. On your next script I would suggest you give it a try if you're ready for it. If you're feeling particularly frisky you can do a search and replace and change all 'pbrand' to 'brand' and all 'pmodel' to 'model', but at this point it might be better to stick with it as is.
Here is my complete code:
<?php
require_once('Connections/mysql.php');
mysql_select_db("tnt_items");
$static_models = array("Audiovox","iPod","Kyocera","LG","Motorola","Nextel","Nokia","Samsung","Sanyo","Siemens","Sony","Treo");
$pbrand = '';
$pmodel = '';
// Specifically for this example this is overkill but always validate your form data
if(isset($_POST['pbrand']))
if(in_array($_POST['pbrand'],$static_models))
$pbrand = $_POST['pbrand'];
$qry = mysql_query("SELECT * FROM mt_tips WHERE model='$pmodel'") or die(mysql_error());
$dat = mysql_fetch_array($qry);
$pimage = $dat['image'];
$pimage2 = $dat['image2'];
$pitem = $dat['item'];
$pcla = $dat['cla'];
$pbt = $dat['bluetooth'];
// collect the models:
$rows = array(); // initialize the variable, not strictly necessary but good practice
while($onemodel = mysql_fetch_row($qry)) { // We'll do this until we run out of data
// The empty brackets say 'the next element's gonna be'
// The [0] is because even though we're just getting one field, it still comes out as an array
$rows[] = $onemodel[0];
} // EndWhile getting records
if(isset($_POST['Submit'])){
if(in_array($_POST['model'],$rows))
$pmodel = $_POST['model'];
$pitem = $_POST['item'];
$pcla = $_POST['cla'];
$pbt = $_POST['bluetooth'];
$pimage = $_POST['image'];
$pimage2 = $_POST['image2'];
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Dynamic select</title>
<style type="text/css">
<!--
#output { border: solid navy 1px; margin-top:10px;}
#output th { border-bottom: solid navy 1px;padding-left:5px; padding-right:5px; }
#output th+th,#output td+td { border-left: solid navy 1px; }
-->
</style>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<select name="brand" onchange="form.submit();">
<option value="">Select</option>
<?php // I dumped them dynamically for brevity
foreach($static_models as $brandname) {
echo " <option value=\"$brandname\"";
if($brandname == $pbrand)
echo " selected=\"selected\"";
echo ">$brandname</option>\n";
}// EndForEach brand
?>
</select>
<?php // Model select appears once brand is selected
if(isset($_POST['brand'])) {?>
<select name="model">
<option value="">Select</option>
<?php // Populate by selected brand
foreach($rows as $model) {
echo " <option value=\"$model\"";
if($model == $pmodel)
echo " selected=\"selected\"";
echo ">$model</option>\n";
}// EndForEach model
?>
</select>
<input type="submit" name="Submit" value="Submit" />
<?php }?>
</form>
<?php // Appears once model is selected
if(isset($_POST['Submit'])) {?>
<p class="headline">Your Results for <? echo $pbrand;?> <? echo $pmodel;?></p>
<table width="100%" border="0" cellpadding="1">
<tr>
<td bgcolor="#CCCCCC"><table width="100%" border="0" align="center" cellpadding="2" cellspacing="2" class="TableContent">
<tr>
<td width="33%" class="TableHeader">Image</td>
<td width="22%" class="TableHeader">Tip Item # </td>
<td width="17%" class="TableHeader">CLA # </td>
<td width="28%" class="TableHeader">Bluetooth Capable? </td>
</tr>
<tr>
<td rowspan="2" bgcolor="#FFFFFF" valign="top"><a href="#" onmouseover="this.style.cursor='pointer'" onmouseout="this.style.cursor='default'" onclick="MM_openBrWindow('<? echo $pimage2;?>','Image','location=yes,scrollbars=yes,resizable=yes,width=600,height=600')"><img src="<?php echo $pimage;?>" /></a></td>
<td align="center" bgcolor="#FFFFFF"><?php echo $pitem;?></td>
<td align="center" bgcolor="#FFFFFF"><?php echo $pcla;?></td>
<td align="center" bgcolor="#FFFFFF"><?php echo $pbt;?></td>
</tr>
<tr>
<td colspan="3" align="center" bgcolor="#FFFFFF"> </td>
</tr>
</table></td>
</tr>
</table>
<?php }?>
</body>
</html>
In your query, you're asking mysql for records where the model is blank.
Then you tell mysql to give you one row of data and you put the values in pimage, pimage2, etc.
Then under '// collect the models', you're telling mysql to give you the rest of the rows (starting with the 2nd row since you already grabbed the first) of blank models.
Since you changed the name of the select to 'brand', change your $_POST['pbrand'] references to $_POST['brand'].
In order, this is what I understand that you want this page to do:
It might help if you break the code into functional sections to address these states. These are actually backward as far as the sequence of events because it makes the IF statements easier to structure:
if(isset($_POST['Submit'])) {
// Someone selected the model,so get that model's data from the database table
} // EndIf model selected
elseif(isset($_POST['brand'])) {
// Someone selected the brand, so get the models for that brand from the database table
} // EndIf brand selected
else {
// If we're in here, that means the visitor just got here
} // EndElse first display of page
Ok? So look at the thread on sql basics, then look at your code and re-organize it into these bite-sized chunks. Inside each chunk perform the steps necessary for the proper display of the page at that point in its sequence.