A question ?!!!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
afshin2740
Forum Newbie
Posts: 3
Joined: Wed Apr 13, 2011 10:29 am

A question ?!!!

Post by afshin2740 »

Hi
I'm new to PHP and I want to write a script to compare two products.
I have a table named mt_entry with 3 colomns named ID, Field_type and Field_content.

Image

As you see in the picture above, I have two products with two different IDs and different specifications. The code I have provided looks like this:

Code: Select all

<?php 
if ( isset ($_GET['submit']) )
{
	$phone1 = mysql_real_escape_string($_GET['phone1']);
	$phone2 = mysql_real_escape_string($_GET['phone2']);
    
	$query1 = mysql_query ("SELECT field_content FROM mt_entry WHERE `id`=$phone1") or die(mysql_error());
	$query2 = mysql_query ("SELECT field_content FROM mt_entry WHERE `id`=$phone2")or die(mysql_error());
	
    $row1 = mysql_fetch_assoc($query1);
	$row2 = mysql_fetch_assoc($query2);
}
?>


The missing part that I can't deal with is querying :

SELECT field_content FROM mt_entry WHERE `id`=$phone1 AND `Field_type`='model'
SELECT field_content FROM mt_entry WHERE `id`=$phone1 AND `Field_type`='brand'
SELECT field_content FROM mt_entry WHERE `id`=$phone1 AND `Field_type`='OS'
SELECT field_content FROM mt_entry WHERE `id`=$phone1 AND `Field_type`='status'

The function that I want from the code is that when the Field_type = model, show the related Field_content .
But having lot's of queries makes code awful. Is there any way to deal with this code ?!
I'm really new to PHP, so I would appreciate your help.
Thanks in advance!
Last edited by afshin2740 on Wed Apr 13, 2011 1:00 pm, edited 1 time in total.
afshin2740
Forum Newbie
Posts: 3
Joined: Wed Apr 13, 2011 10:29 am

Re: A question ?!!!

Post by afshin2740 »

Nobody wants to help ?!
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

Re: A question ?!!!

Post by danwguy »

Your original post should select everything in the database associated with that phone, you just need to add a while loop and echo the content. I don't quite understand why you would need multiple SELECT statement on the db, you have it right so far, aside from ...

Code: Select all

$query1 = mysql_query ("SELECT * FROM mt_entry WHERE `id`=$phone1") or die(mysql_error());
        $query2 = mysql_query ("SELECT * FROM mt_entry WHERE `id`=$phone2")or die(mysql_error());
change to...

Code: Select all

$query1 = mysql_query ("SELECT * FROM mt_entry WHERE id='$phone1'") or die(mysql_error());
        $query2 = mysql_query ("SELECT * FROM mt_entry WHERE id='$phone2'")or die(mysql_error());
and then add a...

Code: Select all

while($row = mysql_fetch_assoc($query1) {
echo "model: " .$row['field_type'];
echo "<br /> " .$row['field_content'];
or something along those lines, but you have the select right.
afshin2740
Forum Newbie
Posts: 3
Joined: Wed Apr 13, 2011 10:29 am

Re: A question ?!!!

Post by afshin2740 »

I think I couldn't make it clear enough.
The query I want is this :

When ID = 1 , and Field_type = model, then show Field_content = E7

When ID = 1 , and Field_type = brand, then show Field_content = NOKIA


When ID = 2 , and Field_type = model, then show Field_content = Xperia

When ID = 2 , and Field_type = brand, then show Field_content = Sony Ericsson

and so on ...
Post Reply