Forum Moderators: coopster

Message Too Old, No Replies

help with for loop and mysql

Please help me

         

Dove457

6:14 pm on Jan 5, 2009 (gmt 0)

10+ Year Member



Hi! I need some help with php and mysql. I have write some code... But don't know, how do that. I need to post mysql records in to table. I do that, but I need to view this in few pages. Becouse, I have abuot 300 records in mysql. I know, i must do this with FOR loop, but how ? Coud you help me ? Sorry for may english... That is my code:

<?php
$db=mysql_connect('localhost','xx','xx');
mysql_select_db('baziukas');

if ($db)
{
echo ("CONNECTED<br>");
$CL=mysql_close($db);
if ($CL="") {
echo ("SOMETING WRONG");
}
if ($CL="1")
echo ("RYSYS BAIKTAS");
}
else
echo ("NOT CONNECTED TO MYSQL!");
?>

<table>
<table border=1>
<tr>
<th>ID</th>
<th>Tekstas</th>
<th>Data</th>
</tr>

<?php
$db = mysql_connect("localhost","xx","xx");
if (!$db)
{
die('NOT CONNECTED!' . mysql_error());
}

mysql_select_db("baziukas", $db);

$result = mysql_query("SELECT * FROM table ORDER BY ID DESC LIMIT 6");
while($row=mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>".$row['id']."</td>";
echo "<td>".$row['date']."</td>";
echo "<td>".$row['psw']."</td>";
echo "</tr>";
$rows[]=$row;
$yrasai=count($rows);
}

$puslapiai=$num_rows/$yraai;
echo $puslapiai;
$page = (isset($_GET['page'])? $_GET['page'] : 1);
$limit = $page * 30 - (30);

echo $page;
mysql_close($db);
?>
</table>

Plz help me :/

[edited by: eelixduppy at 6:30 pm (utc) on Jan. 5, 2009]
[edit reason] removed db specifics [/edit]

ag_47

12:41 am on Jan 6, 2009 (gmt 0)

10+ Year Member



Wait, this is a single file? Why are are you connecting to mysql server twice? Try

<?php
$db=mysql_connect('localhost','xx','xx');
if($db == false){
echo 'NOT CONNECTED TO MYSQL!';
exit(); //or do something else
}
mysql_select_db('baziukas');
?>
... html
<?php
$result = mysql_query("SELECT * FROM table ORDER BY ID DESC LIMIT 6");//Select everything?
while($row=mysql_fetch_assoc($result)) { //use mysql_fetch_assoc
...

Hope that helps

Dove457

4:52 pm on Jan 6, 2009 (gmt 0)

10+ Year Member



Coud you tell me how i can do pagenation ? plz :/

cameraman

5:05 pm on Jan 6, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Move the lines where you're figuring out the page to above your sql statement and modify the statement to use the calculated limit:
$page = (isset($_GET['page'])? $_GET['page'] : 1);
$limit = $page * 30 - (30);

$result = mysql_query("SELECT * FROM table ORDER BY ID DESC LIMIT $limit,30");
while($row=mysql_fetch_array($result))
{

Dove457

5:21 pm on Jan 6, 2009 (gmt 0)

10+ Year Member



I know this... But I need to show results in some few pages. Egzammle...Browsver go to ?page=2 ?page=3, when clik page 1,2,3... I need this... Plz help guys :/

coopster

1:27 pm on Jan 7, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Hi Dove457 and welcome to WebmasterWorld.

cameraman is showing you exactly that in his example. Then you build your "previous" and "next" links in your html based on the current page.

ag_47

4:43 am on Jan 8, 2009 (gmt 0)

10+ Year Member



Just Google "tutorial PHP search display pages", there are a lot of nice tutorials explaining this process in detail.