php Listing


The presentation of lists with the results of the adverts.

Posted on June  14th, 2009 by Technology Department 

It’s now time to show the results of the announcement placed by our students and by users in general. Let’s suppose that we want to show a list of all the adverts that are classified as computer adverts, for this we need to consult Mysql again to call up all the records (identifiers id) whose ‘type of object’ field is computer.

 

Simplified code to show results Mysql

In the first part of the code we must simply repeat what we did before, that is, we have to open the php, assign a variable $code el valor computer and incluide the file with the data from the database.

<?php

$code = “computer”;

include(“config.inc.php”);
$conexion = mysql_connect($db_host,$db_user,$db_pass);
mysql_select_db(“$db_name”, $conexion);

Next, we’ll take the table and select the last 10 records. The way of saying this is by using the annotation ORDER BY id DESC LIMIT 10. If we want 20 records on the paeg we just have to change it to DESC LIMIT 20.
Remember that we name the variables $queEmp o $resEmp whatever we want. You could call then $cadena1 o $busca-datos, etc. if you liked

$queEmp = “SELECT * FROM $db_table WHERE field_6 = ‘$code’ ORDER BY id DESC LIMIT 10 “;
$resEmp = mysql_query($queEmp, $conexion) or die(mysql_error());
$totEmp = mysql_num_rows($resEmp);

A ‘While’ appears in the next line,this is while what is in brackets is done. This function recuperates a row as an associative matrix. Imagine that, while you were looking in the database, the first indentifier fulfills the condition that field 6 must contain the word computer. This instruction colelcts all the data that there is in this row, from the first field to the last, and presents it in an array of data. Now we have an array with all this identifier’s (id) information ready to be shown on screen. Firstly, we generate a table in html and to do this we close php and add the required html code in accordance with what the student wants. This table can contain however many fields, rows and columns as you like and you can colour the cells, add borders to the table etc, etc. This is a html thing and can be done using a html editor like nvu. As it is not the topic of this chapter, we’re going to move back to the what is essential to the topic.

while ($rowEmp = mysql_fetch_assoc($resEmp)) {?>

<table border=”1″ width=”92%” bordercolor=”#333399″ style=”border-collapse: collapse”>
<tr>
<td width=”165″ bgcolor=”#E6E0C0″ bordercolorlight=”#FF9933″ bordercolordark=”#FF6600″ bordercolor=”#FF6600″ rowspan=”2″>
<font size=”1″>

<img border=”0″ src=”images/schedule.png” width=”27″ height=”26″ align=”left” hspace=”1″></font><p>
<font size=”1″>

The first line that we see where the first datum of the database is generated is the following

<? echo $rowEmp[‘field_10’];?>

It’s very simple. The first thing to do is open php, then we tell it to show the value of the variable $rowEMP, which has been taken from the database, on the screen. But, because this variable is an array varaible (it has many values according to position or value) we select the field_10 that corresponds to the date we have stored.
In this way we can access all the data, like the price, description etc.
I want to make it clear that this array variable has no value like como $dato1 = “344”;but it can contain many values and it is because of this that these variables are called multidimensionals.
The rest of the code is easy to understand because we just have to apply our former criteria.

</font></p>
<p>
<font size=”1″>
<?
$base = “https://www.petervaldivia.com/technology/php/board/”;
//Vamos a quitar el enlace desde donde esta la foto y tomar la foto pequeña
// para ello le quitamos el directorio y le ponemos el nuevo directorio con la foto pequeña
// la imagen 1 dara la ruta de la foto grande
$image1 = $rowEmp[‘field_9’];

$image2 = substr ($image1, 7);
$image3 = “thumbs/”.$image2;

echo “<a href=# onclick= abrirpopup(‘$image1′,500,500)> <img src=’$image3′ border=’1′ class= borde></a>”;
//echo “<a target= _blank href=’$image1′> <img src=’$image3′ border=’1’ class= borde></a>”;

?></font></p>
<td bgcolor=”#B6E5FF”>
<p style=”margin-top: 0; margin-bottom: 0″><font size=”2″>

</span> </font><b>
<font size=”2″>
<img border=”0″ src=”images/money-bag.png” width=”32″ height=”32″ align=”left”> <? echo $rowEmp[‘field_11’];?> €</font></b><font size=”2″><b><span lang=”es”>
</span></b> </font> </p>
<p style=”margin-top: 0; margin-bottom: 0″><font size=”2″><b>
<font color=”#FF3300″>Title</font></b><font color=”#FF3300″></b></font>:<b><font color=”#333399″> <? echo $rowEmp[‘field_8’];?>
</font></b> </font> </p></td>
</tr>
<tr>

<td bgcolor=”#B6E5FF”>
<p style=”margin-top: 0; margin-bottom: 0″><b>
<font size=”2″ color=”#FF3300″>Description</font></b><font size=”2″>: <?
// vamos a reducir el texto a poner a 400 caracteres
$string1 = $rowEmp[‘field_7’];

$description = substr($string1,0,400);

echo $description;?>
</font><b>
<font size=”2″>&nbsp;<font color=”#FF3300″>Owner</font>:<? echo $rowEmp[‘field_1’];?> </font>
</b>

<img border=”0″ src=”images/HP-Mobile-2.png” width=”32″ height=”32″ > <? echo $rowEmp[‘field_5’];?>
<img border=”0″ src=”images/address-book-2.png” width=”32″ height=”32″> <? echo $rowEmp[‘field_3’];?>
<b>
<span lang=”es”>
<font size=”2″>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

</font>
</p></td>
</tr>
</table>
<?

}
}
?>

In the former code substr($string1,0,400);-> takes the first 400 values, counting from the value 0, from the variable ( $string1 ). It does this to limit the number of words that can be shown.