Hi,

I'm trying to do a table that shows all the news:

<?PHP
include "db_connect.php";

$sql_destaque = "SELECT data_destaque, imagem_destaque, descricao_destaque FROM destaques ORDER BY cod_destaque ASC";
$executa=mysql_query($sql_destaque,$connect);

$dados = array();
while ($linha = mysql_fetch_array($executa)) $dados[] = $linha;
mysql_free_result($executa);


function add_imglink($linha) {
    echo '<td align="center">';
            echo $linha['data_destaque']."<br/>".$linha['descricao_destaque'];          
			echo '<img src="', $linha['imagem_destaque'], '"><br />';
    echo '</td>';
}


define('NUMERO_COLUNAS', 4);
echo '<table width = 90%  height = 45% align = center>';
for ($i = 0; $i < count($dados); $i += NUMERO_COLUNAS) {
    echo '<tr>';

    for ($col = 0; $col < NUMERO_COLUNAS; $col++) {
        if ($i + $col < count($dados)) add_imglink($dados[$i + $col]);
    }

    echo '</tr>';
}
echo '</table>';

But it gives me error at:

while ($linha = mysql_fetch_array($executa)) $dados[] = $linha;
mysql_free_result($executa);

Can someone help me, please?


Thank You,
PF2G

Member Avatar
diafol
while ($linha = mysql_fetch_array($executa)){
 $dados[] = $linha;
}

Maybe? Otherwise you may have an error in the query - check for number of rows with mysql_num_rows too.

while ($linha = mysql_fetch_array($executa)){
 $dados[] = $linha;
}

Maybe? Otherwise you may have an error in the query - check for number of rows with mysql_num_rows too.

It gives me error in:

while ($linha = mysql_fetch_array($executa))

ERROR:

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wamp\www\site\index.php on line 135

Member Avatar
diafol

As I mentioned, check with mysql_num_rows(). BUT, I suggest you check your query with static values in the phpmyadmin SQL box.

As I mentioned, check with mysql_num_rows(). BUT, I suggest you check your query with static values in the phpmyadmin SQL box.

It's working.

Now can you help me with other thing?

Now i want to if it reaches to 5 news, the news number 1 is erased.

Do you understand?

Member Avatar
diafol

no

no

Let's see...

You have 4 news, right? And then i add another one, that is automatically insert in the website.

Do you understand 'till here?

Member Avatar
diafol

So you just want the last 4, not the first?

If so, use the LIMIT clause:

SELECT ... FROM table ORDER BY id DESC LIMIT 4