Comparasion between products

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
lokodaweb
Forum Newbie
Posts: 2
Joined: Thu Sep 02, 2010 8:39 pm

Comparasion between products

Post by lokodaweb »

db.sql
-- phpMyAdmin SQL Dump
-- version 3.2.3
-- http://www.phpmyadmin.net
--
-- Servidor: localhost
-- Tempo de Geração: Set 02, 2010 as 11:09 PM
-- Versão do Servidor: 5.1.40
-- Versão do PHP: 5.2.11

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";

--
-- Banco de Dados: `teste`
--

-- --------------------------------------------------------

--
-- Estrutura da tabela `info`
--

CREATE TABLE IF NOT EXISTS `info` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`info` varchar(50) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ;

--
-- Extraindo dados da tabela `info`
--

INSERT INTO `info` (`id`, `info`) VALUES
(1, 'Compacto'),
(2, 'Leve'),
(3, 'HD'),
(4, 'FULL HD'),
(5, '1366x768'),
(6, '1920x1080'),
(7, 'Entrada HDMI'),
(8, 'DTV');

-- --------------------------------------------------------

--
-- Estrutura da tabela `info_produto`
--

CREATE TABLE IF NOT EXISTS `info_produto` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`info` varchar(50) NOT NULL,
`id_produto` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ;

--
-- Extraindo dados da tabela `info_produto`
--

INSERT INTO `info_produto` (`id`, `info`, `id_produto`) VALUES
(1, 'Compacto', 4),
(2, 'HD', 4),
(3, '1366x768', 4),
(4, '1920x1080', 1),
(5, 'FULL HD', 1),
(6, 'DTV', 1),
(7, '1920x1080', 3),
(8, 'FULL HD', 3);

-- --------------------------------------------------------

--
-- Estrutura da tabela `produto`
--

CREATE TABLE IF NOT EXISTS `produto` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`nome` varchar(50) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

--
-- Extraindo dados da tabela `produto`
--

INSERT INTO `produto` (`id`, `nome`) VALUES
(1, 'TV Plasma 70'),
(2, 'TV lcd 42'),
(3, 'TV lcd 50'),
(4, 'TV lcd 32');
index.php

Code: Select all

<?
$servidor 	= "localhost";
$basedados 	= "teste";
$usuario 	= "root";
$senha 		= "root";

//conexão com o servidor
$conn = @mysql_connect($servidor, $usuario, $senha)or die (mysql_error());

//conexao o Banco de Dados.	
$db = @mysql_select_db($basedados, $conn) or die (mysql_error());
?>	
<table border="1">
<tr>
<td>
Caracteristicas
</td>
<?  
$id = "'4','3','1'";
  
//Aqui faco o select dos produtos, usando o IN para pegar varios registros
$sql = mysql_query("SELECT * FROM produto WHERE id IN ($id)") or die (mysql_error());  
while ($rs = mysql_fetch_array($sql)) {
?>
<td>
<table border="1">
<?
//Aqui faco o select da info
$sql_info = mysql_query("SELECT * FROM info ORDER BY info ASC") or die (mysql_error());
while ($rs_info = mysql_fetch_array($sql_info)) {

//Aqui faco o select das info dos produtos usando o IN
$sql_info_prod = mysql_query("SELECT * FROM info_produto WHERE id_produto IN ($id)")or die (mysql_error());                                                                                                                                    
while ($rs_info_prod = mysql_fetch_array($sql_info_prod)) {

//aqui faco a verificacao se o ID do produto e igual ao da info e as info sao iguais, mostra o ON
if($rs['id'] == ($rs_info_prod['id_produto']) && $rs_info['info'] == ($rs_info_prod['info'])) {
echo '<tr><td>';
echo '<b>'.$rs_info_prod['info'].'</b>';
echo '</td></tr>';
//aqui faco a verificacao caso o ID seja diferente e as info iguais, mostrara o OFF
}elseif ($rs['id'] != ($rs_info_prod['id_produto']) && $rs_info['info'] == ($rs_info_prod['info'])) {
echo '<tr><td>';
echo '<s>'.$rs_info_prod['info'].'</s>';
echo '</td></tr>';
}
}
}?>    
</table>
</td>
<?}?>
</tr>
</table>
To make easy the understood about my situation, I´ll available some pictures.

The example below it´s the comparion between the ID 3 (TV lcd 50) and ID 2 (TV lcd 42)

Image

As you can notice the comparation is correct, because not there is repeat the data.

The example below it´s the comparion between the ID 3 (TV lcd 50) and ID 1 (TV Plasma 70)

Image

As you can notice 1920x1080 and FULL HD repeated as much as exist as not exist
The idea it´s that remain just 'exist' and not repeat the 'not exist'

One more example between ID 4,3 and 1

Image

As you can notice 1920x1080 and FULL HD repeated as much as exist as not exist

That´s is my original idea:

Image

So, I guess that must solve in the condition IF,...

Thanks you guys !
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Comparasion between products

Post by Jonah Bron »

This is redundant. Instead of
INSERT INTO `info_produto` (`id`, `info`, `id_produto`) VALUES
(1, 'Compacto', 4),
(2, 'HD', 4),
(3, '1366x768', 4),
(4, '1920x1080', 1),
(5, 'FULL HD', 1),
(6, 'DTV', 1),
(7, '1920x1080', 3),
(8, 'FULL HD', 3);
You should change it to
INSERT INTO `info_produto` (`id`, `info`, `id_produto`) VALUES
(1, 1, 4),
(2, 3, 4),
(3, 5, 4),
(4, 6, 1),
(5, 4, 1),
(6, 8, 1),
(7, 6, 3),
(8, 4, 3);
You can see that the `info` column now points to the `info` table's `id` column. Then, just use a JOIN in your SQL statement.

Code: Select all

$sql_info_prod = mysql_query("SELECT * FROM info_produto INNER JOIN info ON info_produto.info = info.id WHERE id_produto IN ($id)")or die (mysql_error()); 
That's a tough logic problem. It would be simpler to do something like this:
[syntax]
product 1 product 2 product 3
feature 1 yes no no
feature 2 no no no
feature 3 yes yes yes
feature 4 on yes yes
[/syntax]
lokodaweb
Forum Newbie
Posts: 2
Joined: Thu Sep 02, 2010 8:39 pm

Re: Comparasion between products

Post by lokodaweb »

Tks Jonah, but not changed nothing, it´s of same way. Your idea about put yes or no it´s great, but check out this website,...

http://www.rossiresidencial.com.br/empr ... idade=9668

This website is what I wish do. Check out Opções de Lazer

I just need that don´t repeat yes as much no, only keep yes.

So just help me about IF, to make of right way.
Post Reply