PHP Again...

Soldato
Joined
26 Nov 2005
Posts
3,839
Location
Doon the Bay (Newcastle)
I'm trying to display more than one bit of information in a dropdown box though all my efforts ends up with a blank

Here's the code:

Code:
<?php
while ($componentdetail = mysql_fetch_array($Cpus)){
	$Compid= $CoName['id'];
	$Model = htmlspecialchars($componentdetail['CompName']);
	echo "<option value='$Model'>$Model</option>\n";
	}
?>

What i want is more details, i've tried the following:

['CompName'] "&" ['Model']
['CompName'] '&' ['Model']
['CompName' . 'Model']
['CompName' '.' 'Model']

I'm no doubt halfway down the wrong road. Anyone point me back on track?
 
When using the WHERE statement i'm also having a weird problem.

Basically if i search a column with a number in it it's fine but when i search a column with text in it doesn't work.

Inside PHPMyAdmin (im using xampp) i get:

#1054 - Unknown column 'INTEL' in 'where clause'

the sql for that:

SELECT * FROM `componentdetail` WHERE `CompName` = INTEL

IF i put this in:

SELECT * FROM `componentdetail` WHERE `Price` = 200

I get a return of records.
 
Intel needs to be surrounded with ' ' as its text whereas in when you are using the price the input is an integer so its fine without the ' ' :)
 
Try:

Code:
SELECT * FROM componentdetail WHERE CompName = 'INTEL'

Regarding your first problem, the variable $componentdetail is an array so you need to be explicit when accessing its members (as Dj_Jestar did when he corrected it). Both Model and CompName are members of the array and don't mean anything on their own. You were trying to take a shortcut but the interpreter/compiler isn't smart enough to figure that out, it's quite a common mistake to make, it's similar to doing this:

Code:
if ($number > 10 && < 20) ....
 
Hi, thanks.

It works in "" rather than '' but thanks for putting me on the right lines.
 
One last one if anyone can still be bothered with me:

My next hurdle is getting a selected field to display more information from the same DB. I can't explain it very well so here's a pic.

34q64ap.jpg


The Bottom component details is where the user will select the component and above will be some text appearing a long description, so the i720 is selected, in the component table is a description which has the long text, how do i get that ti display for each individual record?

Oh can you add pictures to a phpmyadmin SQL DB i've like to do the same thing where the picture is of the CPU so each record has its own pic.

Cheers. :D

(Even if its just pointing me to some sites where i can read up and work it out on my own and come back with smaller questions)
 
If I understood correctly you want to be able to click on something in the drop down list and the information will be displayed above (without a page refresh)? In this case you will need to use JavaScript and possibly AJAX if you don't want to be loading all of the information with the page.

As for the second part, you can store images in an SQL database as a BLOB but it's generally better to store just the path in a field which points to the image on the webserver.
 
Thanks for the info, will have a scout about to see if i can make sense of AJAX if not a rethink may be needed.

Cheers.
 
Might be worth considering a JavaScript library such as JQuery to help you out rather than having to reinvent the wheel. JavaScript libraries are usually heavily tested across many platforms so you can pretty much guarantee it'll be optimised code that works on all browsers.

  • First you want to add a listener for an "onChange" event to the drop down, this triggers when the selection changes.
  • The listener will then call a JavaScript function.
  • The function looks at the currently selected item (you may want to change your dropdown to include the item ID as the value rather than the description which you are currently using).
    Code:
    echo "<option value='$Model'>$Model</option>\n";
    echo "<option value='[COLOR="Red"][B]$Compid[/B][/COLOR]'>$Model</option>\n";

  • It then makes an AJAX call to a PHP script on the server and passes along the id. (eg. getinfo.php?item_id=54)
  • The server then sends back a response containing the description and image url in a format known as JSON.
  • The JavaScript function then alters the required elements on the page to make the necessary changes based on the information it got back from the server.

This may sound really complicated but with a JavaScript library such as JQuery it's really quite easy. You will need to write a PHP script that accepts AJAX requests and spits out a JSON string but most tutorials covering AJAX will mention this.
 
If you don't wanna go to the effort of using JQuery (although it would be the better one) you can just load all the values into a hidden field then manipulate what is shown with JavaScript.
 
Ok this is good information, i will try to see what i can come up with today, I can only really afford to spend a day on it, if it doesn't work i'll have to rethink this page.

Cheers.
 
Having a first bash:

This:

<script type="text/javascript" src="jquery.js"></script>

<script type="text/javascript">
$("select").change(function () {
var str = "";
$("select option:selected").each(function () {
str += $(this).text() + " ";
});
$(div class="container").text(str);
})
.change();

</script>

or this

Code:
 <script type="text/javascript">
	$("select").change(function(){
  alert(CompID.id);
}); 
      
    </script>

Seems to be the main direction forums are pointing me in; i won't pretend to have the first clue what's going on.
 
I couldn't get this resolved. Thinking of going for a refresh php instead. Not technically great but hopefully i might know how to do it.
 
Back
Top Bottom