6
I’m developing a website that has a div
where I have a script that lists results of an SQL query. These are images, in this case. The site was developed on just one page, so by clicking on the portfolio, it just takes me to div #portifolio
. The problem is that I would like to change the category on portfolio
that he didn’t give refresh on the page, only the div
.
I already searched a lot on the net, I even found a script that was very similar to what I wanted, but gave bug in modal window and effect Grayscale also.
The code is this:
<div id="portifolio" class="grid_24">
<nav id="menu-portifolio" class="grid_24">
<ul id='nav'>
<li><a href='#'>SERVIÇOS<img class='icon-menu' src='images/icon-menu.png' alt='Icone do menu'/></a>
<ul>
<?php
// Includa a conexão com o banco de dados sql
include "conn.php";
// Conecta a tabela categorias
$result = mysql_query('SELECT * FROM categorias')or die(mysql_error());
echo "<li><a href='?id_todos=todos#bloco03'>   Todos </a></li>";
// Exibe o resultado da consulta
while ($row = mysql_fetch_object($result)) {
echo "<li>
<a href='?id=$row->id#bloco03'>   $row->nome </a>
</li>
";
}
?>
</ul>
</li>
</ul>
</nav>
</nav>
<!-- Primeira pagina portifólio -->
<div id="modal" class="conteudo-portifolio grid_24">
<?php
// Conecta a tabela portifolio
if (isset($_GET['id']) && is_numeric($_GET['id'])) {
$id_categoria = $_GET['id'];
$resultado = mysql_query("SELECT * FROM portifolio WHERE id_categoria = '$id_categoria' LIMIT 12");
// Exibe o resultado da consulta
$myModal = 1;
while ($row = mysql_fetch_object($resultado)){
echo "
<figure class='img-portifolio'>
<a href='#janela1".$myModal."' rel='modal'><img class='' src=". $row->imagem ." alt=". $row->titulo ."/></a>
</figure>
<div class='window' id='janela1".$myModal."'>
<img class='img-thumb' src=". $row->imagem ." alt=". $row->titulo ."/>
</div>
<!-- mascara para cobrir o site -->
<div id='mascara'></div>
";
$myModal++;
}
} else {
$resultado_todos = mysql_query("SELECT * FROM portifolio LIMIT 12");
// Exibe o resultado da consulta
$myModal = 1;
while ($row = mysql_fetch_object($resultado_todos)){
echo "
<figure class='img-portifolio'>
<a href='#janela1".$myModal."' rel='modal'><img class='' src=". $row->imagem ." alt=". $row->titulo ."/></a>
</figure>
<div class='window' id='janela1".$myModal."'>
<img class='img-thumb' src=". $row->imagem ." alt=". $row->titulo ."/>
</div>
<!-- mascara para cobrir o site -->
<div id='mascara'></div>
";
$myModal++;
}
}
?>
</div>
</div>
</div>
And this is jQuery:
<script type="text/javascript">
$(document).ready(function(){
var content = $('#portifolio');
//pre carregando o gif
loading = new Image(); loading.src = 'images/loading_ajax.gif';
$('#portifolio a').live('click', function( e ){
e.preventDefault();
content.html( '<img src="images/loading_ajax.gif" />' );
var href = $( this ).attr('href');
$.ajax({
url: href,
success: function( response ){
//forçando o parser
var data = $( '<div>'+response+'</div>' ).find('#portifolio').html();
//apenas atrasando a troca, para mostrarmos o loading
window.setTimeout( function(){
content.fadeOut('slow', function(){
content.html( data ).fadeIn();
});
}, 500 );
}
});
});
});
</script>
Can you put an example of your code here? The part of the ajax call and the code of the link that calls ajax makes all the difference to be able to answer.
– Sergio
I put the code!
– Hermes Netto
PHP code and HTML/jQuery are in the same file?
– Sergio
Yes! All in the index.
– Hermes Netto
Nostalgia MVC :)
$id_categoria = $_GET['id'];
- Off: Be careful with lines where you take what the user sends and send directly to the database. I recommend a read of this article.– Calebe Oliveira