PHP MYSQL code problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mendoz
Forum Newbie
Posts: 1
Joined: Mon Sep 25, 2006 6:40 am

PHP MYSQL code problem

Post by mendoz »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hey there.

I'm having a difficulty with a php mysql script.

I need a page which shows only 6 pictures.
The adresses of the images are stored in a database.

let's say I have 10 images. 
I want the first page to link to a new page with the remaining four images.

[b]first page:[/b]

Code: Select all

[image] [image] [image] 
[image] [image] [image] 
next
second page:

Code: Select all

[image] [image] [image]  
[image]
previous
Here is the code I got from another forum:

Code: Select all

<?php
mysql_connect( "localhost", "username", "password" ); // Use correct stuff there
mysql_select_db( "images" ); // Use Database Name
$max_images = 8;
if( !isset( $_GET[ 'page' ] ) ) {
$page = 1;
}
else {
$page = $_GET[ 'page' ];
}
$from = ($page * $max_images) - $max_images;

// select the image url's from the db, i_id = the id of the row in the mysql db
$sql = "SELECT * FROM images ORDER BY i_id LIMIT $from, $max_images";
$s = mysql_query( $sql );
while( $res = mysql_fetch_array( $s ) ) {
$i = 1;
//example variables, might be different for you

// assuming the row name is "src"
$src = $res[ 'src' ];

echo '<img> ';
if( $i > 4 ) {
 echo '<br>';
}
$i++;
}
echo '<br>';
$total_images = mysql_result( mysql_query( "SELECT COUNT( i_id ) as Num FROM images" ), 0 );
$total_pages = ceil( $total_images / $max_images );
// previous page
if($page > 1){
 $prev = ( $page - 1 );
   echo '<a>Previous</a> ';
}

for( $i = 1; $i <= $total_pages; $i++ ) {
if( ( $page ) == $i ) {  
 echo "$i ";
}
else {
 echo '<a>$i</a> ';
}
}
// next page
if( $page </a>
Now, I don't want an image, I want an include.
The include is a table with variables (for the image and link) - table.txt.
I want only to show up to 6 includes in a page.
If there are no more includes I want to include 'soon.txt'

First page:

Code: Select all

| include 'table.txt' | | include 'table.txt' | | include 'table.txt' |
| include 'table.txt' | | include 'table.txt' | | include 'table.txt' |
next page
Second page:

Code: Select all

| include 'table.txt' | | include 'table.txt' | | include 'table.txt' |
| include 'table.txt' | | include 'soon.txt' |  | include 'soon.txt' |
previous page
Hope you undestood and could help,
Dror Wolmer


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post by Mordred »

You might want to name these *.php instead of *.txt, otherwise they could be accessible through the web (if they are under the htdocs root), which is generally not a good idea.

As for your coding trouble, read up on integer division and modulus

http://www.php.net/manual/en/language.o ... hmetic.php
http://bg.php.net/ceil, floor, round etc.


Basically if you have $x images

ceil($x/6) will be the number of pages
floor($x/6) will be the number of full pages, and the remaining page will have $x%6 entries.

Repeat the same with 3 instead of 6 for the rows ;)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Example code may be of interest too. The first two links of Useful Posts would be the ones to read.
Post Reply