php code showing as html text on front end

omg! i just entered wrong verificataion code and it deleted my whole post… well what a great start :slight_smile:

Anyway, hi everybody :slight_smile:
This is my first post.

I’ve started using php only recently, after reading recommended books (incl. php and mysql web development by addison-wesley) and watching comprehensive video tutorials (CBT Video2Brain etc. if that rings a bell), which took me many many hours.

Now I want to see if I understood it properly and want to create a search form from scratch, that displays search results underneath it.
This search page should also then be integrated into a Joomla CMS based website (starting a business with a friend) -> would be nice if anyone could tell me if it’ll work without problems, and if not, what should be done so it does work.

Now this is the problem I’m having:

The php code in the file is showing up as text in the html document when i open it in a browser.
Am using xampp package with Apache server 2 and php 5.2.0 (are these maybe the problem?).
Any suggested improvements/fixes to the code would be really appreciated.

Here’s the code:

[php]

Search Parts Ricerca Componenti

p.f. scegliere prima categoria

-- Categoria -- <?php if (isset($_POST['categoria']) && $_POST['categoria'] == 'frizioni') { echo 'selected="selected"'; } ?>Frizioni <?php if (isset($_POST['categoria']) && $_POST['categoria'] == 'marmitte') { echo 'selected="selected"'; } ?>Marmitte <?php if (isset($_POST['categoria']) && $_POST['categoria'] == 'pinzeFreni') { echo 'selected="selected"'; } ?>Pinze Freni <?php if (isset($_POST['categoria']) && $_POST['categoria'] == 'catalizzatori') { echo 'selected="selected"'; } ?>Catalizzatori

Modello

Sgl. Motore

Cilindrata

Anno


<?php if (isset($_POST['categoria']) && $_POST['categoria'] != '') { try { $db = new MySQLi ('localhost', 'Gabriel', 'AK35GTR2'); $db->select_db(autoricambi);

$categoria = $_POST[‘categoria’];
$sql = “SELECT * FROM “.$categoria.” WHERE 1=1”;

if (isset($_POST[‘modello’]) && !is_array($_POST[‘modello’]) && $_POST[‘modello’] != ‘’) {
$modello = trim($_POST[‘modello’]);
$sql .= " AND modello LIKE ‘%".$modello."%’";
}
if (isset($_POST[‘siglaMotore’]) && !is_array($_POST[‘siglaMotore’]) && $_POST[‘siglaMotore’] != ‘’) {
$siglaMotore = trim($_POST[‘siglaMotore’]);
$sql .= " AND sig_motore LIKE ‘%".$siglaMotore."%’";
}
if (isset($_POST[‘cilindrata’]) && !is_array($_POST[‘cilindrata’]) && $_POST[‘cilindrata’] != ‘’) {
$cilindrata = trim($_POST[‘cilindrata’]);
if (eregi(’^[0-9]{3,4}$’, $cilindrata)) {
$sql .= " AND cilindrata LIKE ‘%".$cilindrata."%’";
}
else {
echo ‘Please enter a valid 3-4 digit number
’;
exit();
}
}
if (isset($_POST[‘anno’]) && !is_array($_POST[‘anno’]) && $_POST[‘anno’] != ‘’) {
$anno = trim($_POST[‘anno’]);
if (eregi(’^[0-9]{4}$’, $anno) && $anno >= 1980 && $anno <= 2011) {
$sql .= " AND anno LIKE ‘%".$anno."%’";
}
else if ($anno < 1980 || $anno > 2012) {
echo ‘Please enter a year between 1980 and 2011
’;
exit();
}
else {
echo ‘Please enter a correct 4 digit year
’;
exit();
}
}
$statement = $db->prepare($sql);
$statement->execute();
$statement->bind_result($nr_articolo, $marca, $mod, $cilin, $an, $sig_motore, $prezzo);
(while $statement->fetch()) {
printf(’

’,
htmlspecialchars($nr_articolo), htmlspecialchars($marca), htmlspecialchars($mod), $cilin, $an, htmlspecialchars($sig_motore), $prezzo);
}

$db->close();
echo ‘Disconnected…
’;

}
catch (Exception $exc) {
echo 'Error: ’ . htmlspecialchars($exc->getMessage());
}
}
?>

Numero Articolo Marca Modello Cilindrata Anno Sigla Motore Prezzo
%s %s %s %i %i %s %d
[/php]

Ah, another thing.

MySQL(i) version 5.0.27 (taken from phpinfo()).

… and when i for example pick Frizioni (clutches) from the Categoria (category) drop down list and click Cerca (Search) button, I don’t get any results.
I already created a ‘autoricambi’ database, with a table called ‘frizioni’ which has already rows with data in it (I created db and table with the phpMyAdmin 2.9.1.1 GUI)

Thanks

If, by this you mean a .html file, then there’s your problem. If you are opening index.html and are seeing PHP code, that is because you need to rename your file to index.php and go to localhost/project/index.php instead in your browser. (the change of extension of course also applies to files other than the index)

Hi Smokey, thanks for your reply :slight_smile:

I maybe didn’t explain it clearly enough; i’m not trying to open an html file, it is a php file.

When I open the file i see the form that is created with the html in the php file, but i also see the php code being displayed.

I uploaded an image of the output i get: http://imageshack.us/photo/my-images/546/searchparts.png/

Okay, this is actually due to the path you’re using. You need to access the file through this URL:

http://localhost/phpTraining/searchParts/searchParts.php

If you had to choose a port other than 80 when installing apache you will need to change it to (assuming port 81) localhost:81/phpTraining…

But 80 is default and the first URL above should work.

Hi Smokey,

lol, thanks a lot, it works now… but I get something weird happening:

Looking at the php snippets that I added to the form at the top of the code; they’re supposed to make sure that when the user clicks the ‘back’ button of the browser, the previous page loads and the already selected fields etc. are still marked (so the user doesn’t have to enter everything again).

Ok, now when i pick an item from the first select list and click on Cerca (search) on the next page the item selected in the first select list isn’t marked anymore and when i open that select list i see i.e. 'selected="selected"Frizioni written… what’s going wrong?

Hope I made myself clear…

Where you have this:

<option value="frizioni"><?php ....?>Frizioni

(and for the other option elements of course, just using this one as an example)
Change it to be like this:

<option value="frizioni" <?php ....?>>Frizioni

This is to ensure that you are putting selected=“selected” inside the option HTML tag.

hi smokey,

thanks a lot again for your help… it’s always the small things that i keep missing… should stop thinking 1st that the problem is the code itself and look at the way it’s written first :stuck_out_tongue:

Btw., another question if you don’t mind:

how can I make this search as good as (or nearly) as searches you normally find in forums etc…
I mean, I don’t know if it’s me, but the regular expressions that I can use somehow don’t always work…

Example: searching the make of the car (Modello).
Say I type in Golf1 (forgetting to put a white space between f and 1, how it’s written in the DB table); when I search it doesn’t give me any result… how can i make the search smarter so it ‘sees’ “Golf1” is the same as “Golf 1”?
Also, say i have a column in the db for ‘year’ of make; the year could be just a single year OR a range of years i.e. 1992-1997.
How can the search understand that when i type in say 1994 it should display also the result with 1992-1997?

Cheers :smiley:

That is a bit complicated.

The simple way is to pre-scan the input search string and add a space before all single numbers. That is a bit of code.

The next option would be to just pop-up a message saying “no entries found for your search criteria, please make sure you spelled it correctly!” and let the user handle the repair…

Good luck with it…

Oh, by the way, google.com will give you tons of samples on that.
(Always use PHP or PHP code as the first part of your search… )
Here’s one: Google search: php mysql search sample
Found this great tutorial: http://devzone.zend.com/26/using-mysql-full-text-searching/#Heading12

;D

Hi Ernie :slight_smile:
Thanks for your reply and sorry for my late answer… have been really busy preparing stuff, since i’m going away on Sunday.

Thanks for the link(s) this MATCH… AGAINST search could be helpful… haven’t yet been able to make it work.

I wouldn’t need to use this match-against everywhere… so it needs to work together with the LIKE comparison.
The only thing I can think of (haven’t tried yet), is to overwrite the previous $sql variable, if a match-against textbox is used.

Hope that link (search) works out for you.
I just remembered the other question: Leaving, short on time, but here is basics for that!

“How can the search understand that when i type in say 1994 it should display also the result with 1992-1997?”

You can do this thru programming. Something like this: (Sorry, I do not have time to play with it today)

let’s say input is $inputyear and data you are checking is $DByear … (All of this depends how your DB setup) No code, just layout on how to do it…

if length of $DByear > 4 then
    $DByear1 = left of $DByear for 4 chars

and $DByear2 = right of $dbyear 4chars

compare min/max: if $inputyear=>$DByear1 AND $inputyear<=$DByear2 then do your work

else
   (Here $DByear is 4 chars long)
   Do your normal compare...

endif

Hey Alex :slight_smile:

Thanks for taking time to explain this… i actually already knew i could do something like this… i was just looking for an inbuilt function that i could call to do that for me :stuck_out_tongue:

I’ve tried using match-against but at the end of the code, where the prepared statement takes place, I get an error that there is no object or that there are more variables than the returned ones from the statement.

Is this because in the match-against search we’re adding another variable to compare? (match-against AS something WHERE match-against).

AND lastly, say i do a search that has no results… when that happens the table print with column headings still is printed and below it it’s empty cause there is no data returned…

I tried using the ‘num_rows > 0’ function to only display the heading when there are at least 1 row returned, but it seems you can’t use it with prepared statements… that means i would have to change it to do i.e. fetch_array or fetch_assoc… do i have to? Or can I do it another way without having to do without prepared statements?

Thanks again for your time :wink:

Okay, we will figure it out! LOL…

First, are you trying to do this totally in SQL. Are you wanting to just do a query that does the compares for you and returns just the needed info?
Or are you trying to do this in a program? This makes a big difference on how we sort this out.

I am gone a lot this weekend, but, will try to check in for you…

Let me know what you are trying to do…

One more thing, I found this site about SQL string coding. It covers just about anything you would need to do in a WHERE clause. IF you use my general layout I sent before and used the correct SQL string codes from this site, you should be able to look for dates from 1952-1999 or whatever. Just have to strip off the first date and last date and compare the two as I started before…

This site is helpful with this subject…

http://sqltutorials.blogspot.com/2007/06/sql-string-functions.html

(Go down a little and you will find the LEFT and RIGHT commands to pull off your dates)

Good luck…

Hi Ernie, thanks for checking in again :slight_smile:

Yes, I’m trying to do the former; if sql can do the checking/calculations for me, then I’m going to use that.
I prefer to do it as simply as possible without having to add not necessary code.

I was also thinking to make this a bit more ‘fancy’ and as efficient/fast as possible for the customer.
I’d like to therefore include js/ajax for the client side, after I’m sure everything works if the client has js turned off for whatever reason (we’ll be dealing with other businesses so that’s pretty much a given).

Cheers!

Sponsor our Newsletter | Privacy Policy | Terms of Service