Log in

View Full Version : Adding columns in script



Bar2aYunie
05-07-2010, 06:10 PM
Hello,

I have a php code which collects info from a database and then display the info in a table with specific details. Anyway, the script works perfectly but I'd like to add another column. Everytime I try, I get an error at the last line of the whole page (Parse error: syntax error, unexpected $end => thus referring to: </html>).

The idea is to have an image and a line of text underneath it in every cell. So it should be:

image1 image2 image3 image4
text1 text2 text3 text4

The script also currently has 4 columns, though the second and fourth are empty. If I do fill them, the info from column one and three is copied into the second and fourth column. That way, I'll get:

image1 image1 image2 image2
text1 text1 text2 text2

Can someboy please help me out to create four columns with in every column different info from my SQL table?

Here's my code (I've marked the table settings in red):




<?php include ("../Connection.txt"); ?>


<?php


$id = $_GET['id'];


$maxcol = 4;

$result = mysql_query("SELECT * FROM Folders order by id desc") or die (mysql_error());

$count = mysql_num_rows($result);

$rows = ceil($count/$maxcol);

echo "<table>";



$limit = 20;

$table = 'Folders';

//get requested page

$page = empty($_GET['page']) ? 1 : (int) $_GET['page'];

//calculate offset

$offset = ($page - 1) * $limit;



//construct query

$query = "Select SQL_CALC_FOUND_ROWS * from $table order by formaat asc LIMIT $limit OFFSET $offset";



//execute query

$result = mysql_query($query) or die (mysql_error());

$cResult = mysql_query("Select found_rows()") or die(mysql_error());

list($count) = mysql_fetch_array($cResult, MYSQL_NUM);



$pageNavBar = getPages($limit, $count, $page);

//start the output

echo <<<HTML

<style type="text/css">

/*style links */

.pageNav a {text-decoration:none; cursor:crosshair; color: #131d31;}

.pageNav a:visited {text-decoration:none; color: #131d31;}

/*make the page numbering smaller generally */

.pageNav{ font-size:smaller;}

.page{ border: thin dotted #060409; background-color:#fdffeb;}

.navi{ padding: 5px; margin: 2px; width: 2em;}

.prev{}

.last{ background-color:;}

.first{ background-color:;}

.next{}

.active {backround-color:grey; color:red;}

.select select {}

.table {font-family: verdana; font-size: 12px;}







</style>

<table width="100%" border="0" rules="groups" table class="table">

<thead>

</thead>

<tbody>

HTML;

while ($row = mysql_fetch_assoc($result)){

echo <<<HTML

<tr>

<td align="center" width="50%"><img src={$row['thumb_formaat']}><br>

{$row['formaat']}<br>

</td>
<td align="left" width="0%"></td>

HTML;

$row = mysql_fetch_assoc($result);

if (!$row){

echo "<td>&nbsp;</td><td>&nbsp;</td>";

} else {

echo <<<HTML

<td align="center" width="50%"><img src={$row['thumb_formaat']}><br>

{$row['formaat']}<br>

</td>
<td align="left" width="0%"></td>

HTML;



} //end if

echo "</tr>";

} //end while



echo <<<HTML

</tbody>

<tfoot>

<tr>

<p><th colspan="4">

$pageNavBar

</th>

</tr>

</tfoot>

</table>

HTML;



function getPages($limit, $count, $page){

//put the url into a variable

$s = "http://" . $_SERVER['HTTP_HOST'] .'/'. ltrim($_SERVER["SCRIPT_NAME"] ,'/');

//calculate the number of pages needed

$nPages = ceil($count/$limit);

//do the first/last prev/next buttons

$first = <<<HTML

<span class="first navi">

<a href="$s?page=1"><<<</a>

</span>

HTML;

$last = <<<HTML

<span class="last navi">

<a href="$s?page=$nPages">>>></a>

</span>

HTML;

if ($page > 1) {

$p = $page - 1;

$prev = <<<HTML

<span class="next navi">

<a href="$s?page=$p"><</a>

</span>

HTML;

} else {

$prev = '&nbsp;';

$first = '&nbsp;';

}

if ($page < $nPages) {

$p = $page + 1;

$next = <<<HTML

<span class="next navi">

<a href="$s?page=$p">></a>

</span>

HTML;

} else {


$next = '&nbsp;';

$last = '&nbsp;';

}


//now construct the pages

//if more than 10 then use a select

if ($nPages > 10){

$output = <<<HTMLJS

<span class="navi select">

<select name="page" onchange="window.location='{$s}?page=' + this.options[this.selectedIndex].value;">

HTMLJS;


for ($p=1; $p <=$nPages; $p++){

$output .= "<option value=\"$p\">$p</option>";

}

$output .= '</select></span>';

} else {

$output = '';

for ($p=1; $p<=$nPages; $p++){

$active = ($p == $page) ? 'active' : '';

$output .= "<span class=\"navi page {$active}\"><a href=\"$s?page=$p\">$p</a></span>";

}

}

return '<div class="pageNav">' . $first . $prev. $output . $next . $last . '</div>';

}

?>


Thank you very much! :D

katierosy
05-14-2010, 12:04 PM
If your database fields are like this and you want to display the records through table
image1 image2 image3 image4 text1 text2 text3 text4

<?
print '<table>';

while($row=mysql_fetch_assoc($result)){
print '<tr>';
print '<td>'.$row['image1'].'<br>'.$row['text1'].'</br>'.$row['text1'].'</td><td>'.$row['image2'].'</br>'.$row['text2']'</td><td>'.$row['image3'].'</br>'$row['text3'].'</td><td>'.$row['image4'].'</br>'.$row['text4'].'</td>';
print '<tr>';
}
print '</table>';
?>

May be you do not want like the abobe , kindly let us know.

Bar2aYunie
05-14-2010, 10:15 PM
Thank you for the reply, it was all the info I needed! I reconstructed my table completely since the previous script I was using just couldn't fit to my needs and was impossible to edit the way I wanted too.

I do have something else to ask... It doesn't really have something to do with the previous question, but maybe you know the answer.

The thing is, there are a lot of rows in the database (around 20 or something) and like 100 entries per table. So I don't wanna show all of the information at once. I mean to show only a a few of the details from the table per page (sorted each time per detail viewing). All info will be shown by an image and some text, which varies of course per category they can sort on. Then, the users can click on the corresponding image/text, which will lead to another page where more info will be shown. This goes on until they finally reach the final page where all of the info from one row will be shown.

Lemme give an example of what I mean.
Let's say there is fruit in the database. The entries in the database will show images, the sizes, the amounts, the colors and the prices of the fruit. So on the first page, they'll be viewing the different kind of fruit available. Then when they select one of the fruits, they'll view the possible sizes. They'll click on the desired size and then they can choose the colors. After selecting the desired colors, they can select the desired amounts. This way, there will be less info per page shown and ppl can make their selection of what they wanna view. After selecting the last part, they'll be taken to the final page, which shows all possible information, plus the prices.

I hope you understand what I'm saying.

Anyway, right now, I just made all of the possible pages, with the corresponging php script on it to collect the right data from the database every time. Now my question... Is there a way I can let the database/script create those pages automatically? Meaning the links and all are up automatically just by one page with one script containing all the info.

Thx in advance.