Jump to content

Solved


mendoz

Recommended Posts

[code]
<?php

mysql_connect( "localhost", "username", "password" ); // Use correct stuff there
mysql_select_db( "databse" ); // Use Database Name
$cat=$_GET['cat'];

// Select total results for pagination
$result = mysql_query("SELECT count(id) FROM $cat");
$num_records = mysql_result($result,0,0);

// Set maximum number of rows and columns
$max_num_rows = 2;
$max_num_columns = 3;
$per_page = $max_num_columns * $max_num_rows;

// Work out how many pages there are
$total_pages = ceil($num_records / $per_page);

// Get the current page number
if (isset($_GET['page'])) $page = $_GET['page']; else $page = 1;

// Work out the limit offset
$start = ($page - 1) * $per_page;

// Select the results we want including limit and offset
$result = mysql_query("SELECT id FROM $cat ORDER BY id LIMIT $start, $per_page");
$num_columns = ceil(mysql_num_rows($result)/$max_num_rows);
$num_rows = ceil(mysql_num_rows($result)/$num_columns);

// Echo the results
echo "<center><table bgcolor=\"#D5D5D5\" border=\"0\">\n";
for ($r = 0; $r < $max_num_rows; $r++){
echo "<tr>\n";
for ($c = 0; $c < $max_num_columns; $c++){ // 1
$x = $r * $max_num_columns + $c;
if ($x < mysql_num_rows($result)){
//                  $y = mysql_result($result, $x, 0); // Commented out so I could show your example
                    $num = mysql_result($result, $x, 0);
                    $id=$x+$start;

                  $y = <<<HTML
<table align="center" border="0" cellpadding="0" cellspacing="0" width="145">
  <tr>
    <td width="16"><img src="about_files/top_lef.gif" alt="a" width="16" height="16">
    </td>
    <td background="about_files/top_mid.gif" height="16">
      <img src="about_files/top_mid.gif" alt="a">
    </td>
    <td width="16">
      <img src="about_files/top_rig.gif" alt="a" width="16" height="16">
    </td>
  </tr>
  <tr>
    <td background="about_files/cen_lef.gif" width="16">
      <img src="about_files/cen_lef.gif" alt="a">
    </td>
    <td align="left" bgcolor="#ffffff" valign="middle">
      <div align="center">
        <a href="movie.php?cat={$cat}&id={$id}">
        <img src="movies_img/{$cat}/thumb/{$num}.jpg" width="114" height="86" border="1"></a>
      </div>
    </td>

    <td background="about_files/cen_rig.gif" width="16">
      <img src="about_files/cen_rig.gif" alt="a">
    </td>
  </tr>
  <tr>
    <td height="16" width="16">
      <img src="about_files/bot_lef.gif" alt="a" width="16" height="16">
    </td>
    <td background="about_files/bot_mid.gif" height="16">
      <img src="about_files/bot_mid.gif" alt="a">
    </td>
    <td height="16" width="16">
      <img src="about_files/bot_rig.gif" alt="a" width="16" height="16">
    </td>
  </tr>
</table>
HTML;

                }
                else {
                  $y = '<table align="center" border="0" cellpadding="0" cellspacing="0" width="145">
      <tr>
        <td width="16"><img src="about_files/top_lef.gif" alt="a" width="16" height="16"></td>
        <td background="about_files/top_mid.gif" height="16"><img src="about_files/top_mid.gif" alt="a"></td>
        <td width="16"><img src="about_files/top_rig.gif" alt="a" width="16" height="16"></td>
      </tr>
      <tr>
        <td background="about_files/cen_lef.gif" width="16"><img src="about_files/cen_lef.gif" alt="a"></td>
        <td align="left" bgcolor="#ffffff" valign="middle"><div align="center">
<img src="html/images/bekarov.jpg" width="114" height="86" border="1"></font></div></td>

        <td background="about_files/cen_rig.gif" width="16"><img src="about_files/cen_rig.gif" alt="a"></td>
      </tr>
      <tr>
        <td height="16" width="16"><img src="about_files/bot_lef.gif" alt="a" width="16" height="16"></td>
        <td background="about_files/bot_mid.gif" height="16"><img src="about_files/bot_mid.gif" alt="a"></td>
        <td height="16" width="16"><img src="about_files/bot_rig.gif" alt="a" width="16" height="16"></td>
      </tr>
    </table>';
                }
echo "<td>";
                echo "$y";
                echo "</td>";
}
echo "</tr>\n";
}

// Echo page numbers
echo "</table>\n";
for ($i=1;$i <= $total_pages;$i++) {
if ($i == $page) echo " $i "; else echo " <a href=\"?cat=$cat&page=$i\">$i</a> ";
}
?>
[/code]
Link to comment
Share on other sites

I think I'll make a page called 'tables.php' with a table in it with two rows and three columns,
inside every cell I'll put an <? include ([color=blue]'table.php[/color]'); ?>.
'tables.php' will connect to a specific database via a var ('tables.php?cat=[color=red]cat1[/color]').
Then I want it to check how many rows are in that database.

I want up to 6 <? include ('[color=blue]table.php[/color]'); ?>. in a page.
So, if for example I have 10 rows in database '[color=green]cat1[/color]',
I'll have a page with 6 <? include ('[color=blue]table.php[/color]'); ?>, and a second page with 4 <? include ('[color=blue]table.php[/color]'); ?>,
And two <? include ('[color=brown]table-no.php[/color]'); ?>,

'tables.php?cat=cat1?id=0':
[table][tr][td]<? include ('[color=blue]table.php[/color]'); ?>,[/td][td]<? include ('[color=blue]table.php[/color]'); ?>,[/td][td]<? include ('[color=blue]table.php[/color]'); ?>,[/td][/tr]
[tr][td]<? include ('[color=blue]table.php[/color]'); ?>,[/td][td]<? include ('[color=blue]table.php[/color]'); ?>,[/td][td]<? include ('[color=blue]table.php[/color]'); ?>,[/td][/tr][/table]
next

'tables.php?cat=cat1?id=1':
[table]
[tr]
[td]<? include ('[color=blue]table.php[/color]'); ?>,[/td]
[td]<? include ('[color=blue]table.php[/color]'); ?>,[/td]
[td]<? include ('[color=blue]table.php[/color]'); ?>,[/td]
[/tr]
[tr]
[td]<? include ('[color=blue]table.php[/color]'); ?>,[/td]
[td]<? include ('[color=brown]table-no.php[/color]'); ?>,[/td]
[td]<? include ('[color=brown]table-no.php[/color]'); ?>,[/td]
[/tr]
[/table]
previous
Link to comment
Share on other sites

Yes, obviously a conditional loop with includes should be fine.

I have something similar to this already, are you displaying the results ordered horizontally or vertically?
[pre]Horizontal:
1  2  3  4
5  6  7  8
9  10  11  12

Vertical:
1  4  7  10
2  5  8  11
3  6  9  12[/pre]
Regards
Huggie
Link to comment
Share on other sites

OK,

Here's the code I came up with, the only problem is you'll have to substitute it with your sql as opposed to mine... but you can see it in action [url=http://www.dizzie.co.uk/php/sbspage2.php]here[/url]

[code]
<?php

// Connect to DB
include('connect.php');

// Select total results for pagination
$result = mysql_query('SELECT count(countryName) FROM tblCountry');
$num_records = mysql_result($result,0,0);

// Set maximum number of rows and columns
$max_num_rows = 2;
$max_num_columns = 2;
$per_page = $max_num_columns * $max_num_rows;

// Work out how many pages there are
$total_pages = ceil($num_records / $per_page);

// Get the current page number
if (isset($_GET['page'])) $page = $_GET['page']; else $page = 1;

// Work out the limit offset
$start = ($page - 1) * $per_page;

// Select the results we want including limit and offset
$result = mysql_query("SELECT countryName FROM tblCountry ORDER BY countryName LIMIT $start, $per_page");
$num_columns = ceil(mysql_num_rows($result)/$max_num_rows);
$num_rows = ceil(mysql_num_rows($result)/$num_columns);

// Echo the results
echo "<table border=\"2\">\n";
for ($r = 0; $r < $max_num_rows; $r++){
echo "<tr>\n";
for ($c = 0; $c < $max_num_columns; $c++){ // 1
$x = $r * $max_num_columns + $c;
if ($x < mysql_num_rows($result)){
//                  $y = mysql_result($result, $x, 0); // Commented out so I could show your example
                  $y = "include('table.php')";
                }
                else {
                  $y = "include('table-no.php')";
                }
echo "<td>$y</td>";
}
echo "</tr>\n";
}

// Echo page numbers
echo "</table>\n";
for ($i=1;$i <= $total_pages;$i++) {
if ($i == $page) echo " $i "; else echo " <a href=\"?page=$i\">$i</a> ";
}
?>
[/code]

Regards
Huggie
Link to comment
Share on other sites

Yeah, you were right.

I posted the same problem on another forum and got this:

[quote]
To give you a literal answer, you need fixed number of iterations and change inclusion file after you ouput the number of rows in the table that fit your category. For example:
[/quote]

[code]
// SELECT COUNT(*) FROM tablename WHERE cat='$category'
// get result into $numrows

define ('MAX_ROWS', 6);
define ('COLUMNS', 3);

$j=0;

echo "<table><tr>\n";

for ($i=0; $i<MAX_ROWS; $i++) {
  if ($i>($numrows-1)) {
    echo '<td>'; include 'table-no.php'; echo '</td>';
  } else {
    echo '<td>'; include 'table.php'; echo '</td>';
  }

  $j++;

  if (($j==COLUMNS) && ($i!=(MAX_ROWS-1))) {
    echo "</tr><tr>\n";
    $j=0;
  }
}

echo '</tr></table>';
[/code]

Then I posted the code you gave me and got this reply.

[quote]
The code you have listed will not work because it echoes the include command into HTML where it has no meaning outside PHP. And by the way, do not include, but rather use different functions.

As for your desired implementation of the code itself, you want to present a table of images (or something), six per page in 3x2 layout, correct? Then you can use the code that I gave you above, except you have to construct proper SQL query which tracks which page it presents and how many rows are per page (6).

Tracking pages is simple, through &page=num (or whatever you want to call it) in the URL. You get the desired page number from $_GET['page'], don't forget to cast it into int, and construct the LIMIT clause:
[/quote]

[code]
//$num_rows contains the total number of rows in the table

$page= (isset($_GET['page'])) ? (int) $_GET['page'] : 1;
if ($page>floor($num_rows/6)) $page= floor($num_rows / 6);
if ($page<1) $page=1;

$start= ($page-1) * 6;

$query= "SELECT data FROM table WHERE cat='$cat' LIMIT $start, 6";

// perform query and display results
[/code]
Link to comment
Share on other sites

[quote]
The code you have listed will not work because it echoes the include command into HTML where it has no meaning outside PHP.
[/quote]

I'm fully aware of that :)

I only put that there for an example.  I don't have any files to include, so it would have been pointless me including them.  I personally don't think you should use include files either, you should construct the tables manually in the php.

Anyway, I'm assuming that although you've posted elsewhere and been told it's not correct (which we already knew) the code itself actually works?

Regards
Huggie
Link to comment
Share on other sites

There are two problems:

1. trouble with the <image src=>
2. pagination doesn't work

Here is an example.
http://10bayad.com/test.php?cat=star

Here is the modified code:

[code]
<?php

mysql_connect( "localhost", "user", "pass" ); // Use correct stuff there
mysql_select_db( "database" ); // Use Database Name
$cat=$_GET['cat'];

// Select total results for pagination
$result = mysql_query("SELECT count(id) FROM $cat");
$num_records = mysql_result($result,0,0);

// Set maximum number of rows and columns
$max_num_rows = 2;
$max_num_columns = 3;
$per_page = $max_num_columns * $max_num_rows;

// Work out how many pages there are
$total_pages = ceil($num_records / $per_page);

// Get the current page number
if (isset($_GET['page'])) $page = $_GET['page']; else $page = 1;

// Work out the limit offset
$start = ($page - 1) * $per_page;

// Select the results we want including limit and offset
$result = mysql_query("SELECT id FROM $cat ORDER BY id LIMIT $start, $per_page");
$num_columns = ceil(mysql_num_rows($result)/$max_num_rows);
$num_rows = ceil(mysql_num_rows($result)/$num_columns);

// Echo the results
echo "<table bgcolor=\"#D5D5D5\" border=\"2\">\n";
for ($r = 0; $r < $max_num_rows; $r++){
echo "<tr>\n";
for ($c = 0; $c < $max_num_columns; $c++){ // 1
$x = $r * $max_num_columns + $c;
if ($x < mysql_num_rows($result)){
//                  $y = mysql_result($result, $x, 0); // Commented out so I could show your example

                  $y = '<table align="center" border="0" cellpadding="0" cellspacing="0" width="145">
      <tr>
        <td width="16"><img src="about_files/top_lef.gif" alt="a" width="16" height="16"></td>
        <td background="about_files/top_mid.gif" height="16"><img src="about_files/top_mid.gif" alt="a"></td>
        <td width="16"><img src="about_files/top_rig.gif" alt="a" width="16" height="16"></td>
      </tr>
      <tr>
        <td background="about_files/cen_lef.gif" width="16"><img src="about_files/cen_lef.gif" alt="a"></td>
        <td align="left" bgcolor="#ffffff" valign="middle"><div align="center">
<img src="$cat/$i.jpg" width="114" height="86" border="1"></font></div></td>

        <td background="about_files/cen_rig.gif" width="16"><img src="about_files/cen_rig.gif" alt="a"></td>
      </tr>
      <tr>
        <td height="16" width="16"><img src="about_files/bot_lef.gif" alt="a" width="16" height="16"></td>
        <td background="about_files/bot_mid.gif" height="16"><img src="about_files/bot_mid.gif" alt="a"></td>
        <td height="16" width="16"><img src="about_files/bot_rig.gif" alt="a" width="16" height="16"></td>
      </tr>
    </table>';
                }
                else {
                  $y = "table-no.php";
                }
echo "<td>";
                echo "$y";
                echo "</td>";
}
echo "</tr>\n";
}

// Echo page numbers
echo "</table>\n";
for ($i=1;$i <= $total_pages;$i++) {
if ($i == $page) echo " $i "; else echo " <a href=\"?cat=$cat?page=$i\">$i</a> ";
}
?>[/code]
Link to comment
Share on other sites

You've got it!... Well done...

The reason you have this tiny error is that you have 1 character wrong, a (?) instead of an (&)...

Where the page numbers are displayed at the bottom of the page, change this line:

[code=php:0]<a href=\"?cat=$cat?page=$i\">[/code]


To this:

[code=php:0]<a href=\"?cat=$cat[/code][b]&[/b][color=red]page=$i\">[/color]


Regards
Huggie
Link to comment
Share on other sites

Wow, it works!

Now, the last thing I need is to get the <image src=> right


[code]
<?php
//the piece of the code which isn't working
      $y = '
<table align="center" border="0" cellpadding="0" cellspacing="0" width="145">
        <tr>
              <td width="16"><img src="about_files/top_lef.gif" alt="a" width="16" height="16"></td>
              <td background="about_files/top_mid.gif" height="16">
                    <img src="about_files/top_mid.gif" alt="a"></td>
              <td width="16">
                    <img src="about_files/top_rig.gif" alt="a" width="16" height="16"></td>
        </tr>
        <tr>
              <td background="about_files/cen_lef.gif" width="16">
                    <img src="about_files/cen_lef.gif" alt="a"></td>
              <td align="left" bgcolor="#ffffff" valign="middle">
                    <div align="center">
    <img src="$cat/$i.jpg" width="114" height="86" border="1"></div></td> //problem

              <td background="about_files/cen_rig.gif" width="16">
                    <img src="about_files/cen_rig.gif" alt="a"></td>
        </tr>
        <tr>
              <td height="16" width="16">
                    <img src="about_files/bot_lef.gif" alt="a" width="16" height="16"></td>
              <td background="about_files/bot_mid.gif" height="16">
                  <img src="about_files/bot_mid.gif" alt="a"></td>
              <td height="16" width="16">
                  <img src="about_files/bot_rig.gif" alt="a" width="16" height="16"></td>
        </tr>
</table>
';

                echo "$y";
?>
[/code]
Link to comment
Share on other sites

OK, try changing to this:

[code]
<?php
$y = <<<HTML
<table align="center" border="0" cellpadding="0" cellspacing="0" width="145">
  <tr>
    <td width="16"><img src="about_files/top_lef.gif" alt="a" width="16" height="16">
    </td>
    <td background="about_files/top_mid.gif" height="16">
      <img src="about_files/top_mid.gif" alt="a">
    </td>
    <td width="16">
      <img src="about_files/top_rig.gif" alt="a" width="16" height="16">
    </td>
  </tr>
  <tr>
    <td background="about_files/cen_lef.gif" width="16">
      <img src="about_files/cen_lef.gif" alt="a">
    </td>
    <td align="left" bgcolor="#ffffff" valign="middle">
      <div align="center">
        <img src="{$cat}/{$i}.jpg" width="114" height="86" border="1">
      </div>
    </td>

    <td background="about_files/cen_rig.gif" width="16">
      <img src="about_files/cen_rig.gif" alt="a">
    </td>
  </tr>
  <tr>
    <td height="16" width="16">
      <img src="about_files/bot_lef.gif" alt="a" width="16" height="16">
    </td>
    <td background="about_files/bot_mid.gif" height="16">
      <img src="about_files/bot_mid.gif" alt="a">
    </td>
    <td height="16" width="16">
      <img src="about_files/bot_rig.gif" alt="a" width="16" height="16">
    </td>
  </tr>
</table>
HTML;

echo "$y";
?>
[/code]

Regards
Huggie
Link to comment
Share on other sites

Seems to work good.

Is there a var in your script whose value is the row number?

I tought it was $i but it wasn't.
<img src="{$cat}/thumbs/{$i}.jpg" width="114" height="86" border="1">

Then I tried using $r but it just gave 0 and 1, i know why.

Maybe I need to add a while function?
Link to comment
Share on other sites

There was, but I didn't think you'd be using it... It's in the code, but it's commented out:

[code]
<?php
$y = mysql_result($result, $x, 0); // Commented out so I could show your example
?>
[/code]

What this does is get the data for a particular cell.  Check the manual for the [url=http://uk.php.net/manual/en/function.mysql-result.php]mysql_result[/url] function.  I've given it three parameters.  The first is the result set to use, the second is the database row number, as you can see, I've used $x as that's my table row number and finally a field offset.  My data was in the first column, so I had no need to offset, hence the 0.

This maybe a little confusing, but you should be OK.

Regards
Huggie
Link to comment
Share on other sites

Something else you should probably change, underneath this code:

[code]
<?php
mysql_connect( "localhost", "user", "pass" ); // Use correct stuff there
mysql_select_db( "database" ); // Use Database Name
?>
[/code]

Replace this:

[code=php:0]$cat=$_GET['cat'];
[/code]

With this:

[code=php:0]if (isset($_GET['cat'])) $cat = $_GET['page']; else $cat = "star";
[/code]

Is this line, all it does is provide a default category incase someone attempts to get to test.php on its own without specifiying one.

Let me know if you're still having problems.

Regards
Huggie
Link to comment
Share on other sites

Well, most of the code seems to work right

The only thing missing is that the first img is 5.jpg and not 1.jpg

http://10bayad.com/test.php?cat=star


[code]
<?php

mysql_connect( "XX", "XX", "XX" ); // Use correct stuff there
mysql_select_db( "10bayad" ); // Use Database Name
$cat=$_GET['cat'];

// Select total results for pagination
$result = mysql_query("SELECT count(id) FROM $cat");
$num_records = mysql_result($result,0,0);

// Set maximum number of rows and columns
$max_num_rows = 2;
$max_num_columns = 3;
$per_page = $max_num_columns * $max_num_rows;

// Work out how many pages there are
$total_pages = ceil($num_records / $per_page);

// Get the current page number
if (isset($_GET['page'])) $page = $_GET['page']; else $page = 1;

// Work out the limit offset
$start = ($page - 1) * $per_page;

// Select the results we want including limit and offset
$result = mysql_query("SELECT id FROM $cat ORDER BY id LIMIT $start, $per_page");
$num_columns = ceil(mysql_num_rows($result)/$max_num_rows);
$num_rows = ceil(mysql_num_rows($result)/$num_columns);

// Echo the results
echo "<table bgcolor=\"#D5D5D5\" border=\"2\">\n";
for ($r = 0; $r < $max_num_rows; $r++){
echo "<tr>\n";
for ($c = 0; $c < $max_num_columns; $c++){ // 1
$x = $r * $max_num_columns + $c;
if ($x < mysql_num_rows($result)){
//                  $y = mysql_result($result, $x, 0); // Commented out so I could show your example
                    $num = mysql_result($result, $x, 0);

                  $y = <<<HTML
<table align="center" border="0" cellpadding="0" cellspacing="0" width="145">
  <tr>
    <td width="16"><img src="about_files/top_lef.gif" alt="a" width="16" height="16">
    </td>
    <td background="about_files/top_mid.gif" height="16">
      <img src="about_files/top_mid.gif" alt="a">
    </td>
    <td width="16">
      <img src="about_files/top_rig.gif" alt="a" width="16" height="16">
    </td>
  </tr>
  <tr>
    <td background="about_files/cen_lef.gif" width="16">
      <img src="about_files/cen_lef.gif" alt="a">
    </td>
    <td align="left" bgcolor="#ffffff" valign="middle">
      <div align="center">
        <img src="{$cat}/thumb/{$num}.jpg" width="114" height="86" border="1">
      </div>
    </td>

    <td background="about_files/cen_rig.gif" width="16">
      <img src="about_files/cen_rig.gif" alt="a">
    </td>
  </tr>
  <tr>
    <td height="16" width="16">
      <img src="about_files/bot_lef.gif" alt="a" width="16" height="16">
    </td>
    <td background="about_files/bot_mid.gif" height="16">
      <img src="about_files/bot_mid.gif" alt="a">
    </td>
    <td height="16" width="16">
      <img src="about_files/bot_rig.gif" alt="a" width="16" height="16">
    </td>
  </tr>
</table>
HTML;

                }
                else {
                  $y = '<table align="center" border="0" cellpadding="0" cellspacing="0" width="145">
      <tr>
        <td width="16"><img src="about_files/top_lef.gif" alt="a" width="16" height="16"></td>
        <td background="about_files/top_mid.gif" height="16"><img src="about_files/top_mid.gif" alt="a"></td>
        <td width="16"><img src="about_files/top_rig.gif" alt="a" width="16" height="16"></td>
      </tr>
      <tr>
        <td background="about_files/cen_lef.gif" width="16"><img src="about_files/cen_lef.gif" alt="a"></td>
        <td align="left" bgcolor="#ffffff" valign="middle"><div align="center">
<img src="html/images/bekarov.jpg" width="114" height="86" border="1"></font></div></td>

        <td background="about_files/cen_rig.gif" width="16"><img src="about_files/cen_rig.gif" alt="a"></td>
      </tr>
      <tr>
        <td height="16" width="16"><img src="about_files/bot_lef.gif" alt="a" width="16" height="16"></td>
        <td background="about_files/bot_mid.gif" height="16"><img src="about_files/bot_mid.gif" alt="a"></td>
        <td height="16" width="16"><img src="about_files/bot_rig.gif" alt="a" width="16" height="16"></td>
      </tr>
    </table>';
                }
echo "<td>";
                echo "$y";
                echo "</td>";
}
echo "</tr>\n";
}

// Echo page numbers
echo "</table>\n";
for ($i=1;$i <= $total_pages;$i++) {
if ($i == $page) echo " $i "; else echo " <a href=\"?cat=$cat&page=$i\">$i</a> ";
}
?>
[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.