Jump to content


Photo

Solved


  • Please log in to reply
25 replies to this topic

#1 mendoz

mendoz
  • Members
  • PipPipPip
  • Advanced Member
  • 118 posts

Posted 25 September 2006 - 12:03 PM

<?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> ";
}
?>


#2 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 25 September 2006 - 12:56 PM

I'm assuming that the current code you have is taking care of the pagination and cells ok, it's just the additonal cells you want filling?

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#3 mendoz

mendoz
  • Members
  • PipPipPip
  • Advanced Member
  • 118 posts

Posted 25 September 2006 - 03:24 PM

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 ('table.php'); ?>.
'tables.php' will connect to a specific database via a var ('tables.php?cat=cat1').
Then I want it to check how many rows are in that database.

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

'tables.php?cat=cat1?id=0':

<? include ('table.php'); ?>,<? include ('table.php'); ?>,<? include ('table.php'); ?>,
<? include ('table.php'); ?>,<? include ('table.php'); ?>,<? include ('table.php'); ?>,

next

'tables.php?cat=cat1?id=1':











<? include ('table.php'); ?>, <? include ('table.php'); ?>, <? include ('table.php'); ?>,
<? include ('table.php'); ?>, <? include ('table-no.php'); ?>, <? include ('table-no.php'); ?>,

previous

#4 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 25 September 2006 - 03:48 PM

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
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#5 mendoz

mendoz
  • Members
  • PipPipPip
  • Advanced Member
  • 118 posts

Posted 25 September 2006 - 05:09 PM

[pre]Horizontal:
1  2  3
4  5  6
[/pre]

Something like this.

I'll owe you big time if you can pull this one!

#6 mendoz

mendoz
  • Members
  • PipPipPip
  • Advanced Member
  • 118 posts

Posted 26 September 2006 - 04:10 AM

bump,

Please tell me if this is against the rules

#7 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 26 September 2006 - 08:59 AM

I'll post my code for you this evening when I get home.

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#8 mendoz

mendoz
  • Members
  • PipPipPip
  • Advanced Member
  • 118 posts

Posted 26 September 2006 - 04:55 PM

Thanks huggie  :)

#9 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 26 September 2006 - 11:32 PM

Hey, haven't been home this evening, but should be able to post tomorrow when I do.

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#10 mendoz

mendoz
  • Members
  • PipPipPip
  • Advanced Member
  • 118 posts

Posted 27 September 2006 - 04:11 AM

No rush man.

I started working on those tables since I acidently destroyed my database.  :P

#11 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 27 September 2006 - 04:24 PM

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 here

<?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> ";
}
?>

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#12 mendoz

mendoz
  • Members
  • PipPipPip
  • Advanced Member
  • 118 posts

Posted 27 September 2006 - 05:41 PM

Huggie, I can't thank you enough.

If you ever need something graphics/web desgin related email me at

dror.wolmer@gmail.com

I'll test this ASAP

thanks

#13 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 27 September 2006 - 05:53 PM

No problem, but I have a feeling you'll be back before it's totally finished ;)

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#14 mendoz

mendoz
  • Members
  • PipPipPip
  • Advanced Member
  • 118 posts

Posted 27 September 2006 - 08:15 PM

Yeah, you were right.

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

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:


// 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>'; 

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

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:


//$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 


#15 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 27 September 2006 - 10:46 PM

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


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
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#16 mendoz

mendoz
  • Members
  • PipPipPip
  • Advanced Member
  • 118 posts

Posted 28 September 2006 - 04:08 AM

Yes huggie, it works.

I'll try not using includes and construct the tables there.

http://10bayad.com/test.php

#17 mendoz

mendoz
  • Members
  • PipPipPip
  • Advanced Member
  • 118 posts

Posted 28 September 2006 - 10:37 AM

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:

<?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> ";
}
?>


#18 steveclondon

steveclondon
  • Members
  • PipPipPip
  • Advanced Member
  • 161 posts

Posted 28 September 2006 - 10:48 AM

had a look, makes it very hard without line numbers

#19 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 28 September 2006 - 10:50 AM

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:

<a href=\"?cat=$cat?page=$i\">


To this:

<a href=\"?cat=$cat
&page=$i\">


Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#20 mendoz

mendoz
  • Members
  • PipPipPip
  • Advanced Member
  • 118 posts

Posted 28 September 2006 - 11:18 AM

Wow, it works!

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


<?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";
?>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users