PHP ODBC - Modify my code so that I get 1 record per page?

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
Cep
Forum Newbie
Posts: 16
Joined: Fri Mar 04, 2005 4:45 am

PHP ODBC - Modify my code so that I get 1 record per page?

Post by Cep »

I have a single php file that selects all the records in a table and then displays that information in a form.

The form however gets repeated for each record in the same browser window.

What I want to do is change this so that 1 record appears for once instance of the form and then use a previous and next button (or links but would prefer buttons) to jump through record to record.

I realise that there is another post in this forum but its using MySQL and sounds like the guy is using more then PHP file I am not. How do I go about doing this? I understand I need a variable for the pages similar to the one in the other thread but err.. I'm lost.

Here is the script I use,

Code: Select all

<?php 

//CONNECTION STRING 

function db() 
    &#123; 
        $dbuser        = ""; 
        $dbpass        = ""; 
        $dbname        = "ordertest"; 
         
        //CONNECTION StrING 
        $db_conn = odbc_connect($dbname, $dbuser, $dbpass) 
        or die ("UNABLE TO CONNECT TO DATABASE"); 
        return $db_conn; 
&#125;//end function db 

// Select all the records and assign them to the form variables 
$sql = "SELECT * FROM tblmanorders"; 

$id = ($sql&#1111;'orderID']); 
$firm = ($sql&#1111;'Firm']); 
$contact = ($sql&#1111;'Contact']); 
$email = ($sql&#1111;'Email']); 
$address = ($sql&#1111;'Address']); 
$telephone = ($sql&#1111;'Telephone']); 
$postcode = ($sql&#1111;'Postcode']); 

// Create a record count 
$sqlcount = "SELECT count(*) FROM tblmanorders"; 

// Default variable for this script/form interaction 
$SCRIPT_NAME = "process_orders.php"; 

// Produce results and post to form for each row in the recordset 

$result = odbc_exec(db(),$sql); 

if ($myrow = odbc_fetch_array($result)) &#123; 
do 
&#123; 
?> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 
<link rel="stylesheet" type="text/css" href="admincss.css"> 
<title>Process Orders</title> 
</head> 
<body> 
<form method="post" action="<?php echo $SCRIPT_NAME; ?>?post=yes"> 
<table width="100%" class="type1"> 
    <tr> 
        <td class="title" align="center"> 
            Process Orders 
        </td> 
    </tr> 
</table> 
<table width="100%"> 
    <tr> 
        <td> 
            <table width="100%" class="type2"> 
                <tr> 
                    <th colspan="4" align="left"> 
                        Contact Details 
                    </th> 
                </tr> 
                <tr> 
                    <td width="15%"> 
                        Firm: 
                    </td> 
                    <td width="37%"> 
                            <? $firm = $myrow&#1111;"Firm"]; ?> 
                            <input name="firm" type="text" value="<?php echo $firm; ?>"> 
                    </td> 
                    <td width="19%"> 
                        Contact: 
                    </td> 
                    <td width="29%">     
                        <? $contact = $myrow&#1111;"Contact"]; ?> 
                            <input name="contact" type="text" value="<?php echo $contact; ?>"> 
                    </td> 
                </tr> 
                <tr> 
                    <td> 
                        Email: 
                    </td> 
                    <td> 
                        <? $email = $myrow&#1111;"Email"]; ?> 
                            <input name="email" type="text" value="<?php echo $email; ?>"> 
                    </td> 
                    <td>                     
                        Telephone: 
                    </td> 
                    <td> 
                        <? $telephone = $myrow&#1111;"Telephone"]; ?> 
                            <input name="telephone" type="text" value="<?php echo $telephone; ?>"> 
                    </td> 
                </tr> 
                <tr> 
                    <td valign="top"> 
                        Address: 
                    </td> 
                    <td> 
                        <? $address = $myrow&#1111;"Address"]; ?> 
                                                <textarea name="address" rows="4" cols="39"><?php echo $address; ?></textarea> 
                    </td> 
                    <td valign="top"> 
                        Postcode: 
                    </td> 
                    <td valign="top"> 
                        <? $postcode = $myrow&#1111;"Postcode"]; ?> 
                            <input name="postcode" type="text" value="<?php echo $postcode; ?>"> 
                  </td>                     
                </tr> 
            </table> 
        </td> 
    </tr> 
</table> 
<table width="100%" class="type1"> 
    <tr> 
        <th colspan="2"> 
            Controls & Options: 
        </th> 
    </tr> 
    <tr> 
        <td align="right"> 
            <input name="previousAdm" type="submit" value="Previous"> <input name="nextAdm" type="submit" value="Next"> 
        </td> 
        <td align="left"> 
                <? $id = $myrow&#1111;"OrderID"];?> 
                        <input type="hidden" value="<?php echo $id; ?>" name="OrderID"> 
        </td> 
    </tr> 
</table> 
</form> 
</body> 
<?php 
&#125; 
while ($myrow = odbc_fetch_array($result)); 
&#125; 
?> 
</body> 
</html>
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

You need to use pagination
$sql = "SELECT * FROM tblmanorders LIMIT $n,1";
Use $n to determine the record number and have it as a link for NEXT - echo "<a href=\"results.php?recordno=$n\">Next</a>";
The Link will be like results.php?recordno=5.
Same for back.
Cep
Forum Newbie
Posts: 16
Joined: Fri Mar 04, 2005 4:45 am

Post by Cep »

You cant use LIMIT with ODBC
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

And theres no other alternative for LIMIT ?
Cep
Forum Newbie
Posts: 16
Joined: Fri Mar 04, 2005 4:45 am

Post by Cep »

Not that I can find unfortuantetly.

But I did have the idea of using the WHERE clause instead.

For example.

have a variable "$pageno" that = 1

If "a form control" = "next"
$pageno = $pagno ++
else $pageno = $pageno --
end if

Then have two buttons on the form,
<input value="next">
<input value="prev">

Then having the values submitted pack to the php when the form is posted.

Then running the result as being

$result = odbc_exec ("select * from table where orderid = $pageno")

But I am not having any luck passing the input button info back to PHP.
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

Cep wrote: But I am not having any luck passing the input button info back to PHP.
What do you mean ? Are you talking abt accessing $pageno ?
replace $pageno with $_GET['pageno']
Cep
Forum Newbie
Posts: 16
Joined: Fri Mar 04, 2005 4:45 am

Post by Cep »

No I mean I don't know how to get the form controls (the buttons) value of "next" or "prev" from the buttons on the form part, to the If statement.

All $pageno is, is a variable for a counter.

So if they press the next button, it sends the value "next" to the if statement and then the if statement changes the counter.

Then when the result is read it produces a select query that will match the orderid of a record to that of the counter.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

$_POST['previousAdm'] or $_POST['nextAdm']
Cep
Forum Newbie
Posts: 16
Joined: Fri Mar 04, 2005 4:45 am

Post by Cep »

Ok I have tried this,

Code: Select all

$mypage = 1;

if (isset($_POST&#1111;'previousAdm'])) &#123;
   $mypage = $mypage++;
&#125; elseif (isset($_POST&#1111;'nextAdm']))&#123;
    $mypage = $mypage--;
&#125; else &#123; $mypage = 1; &#125;

if ($mypage=0) &#123;
$mypage = 1;

$result = odbc_exec(db(),"SELECT * FROM tblmanorders WHERE orderID ="."$mypage");
But nothing is happening? I think that the if statement is not working.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

$mypage is being reset to zero all the time.. = vs. == issue. You can actually remove that bit of the code, as $mypage will always be 1, if it wasn't set already from the next/previous stuffs
Cep
Forum Newbie
Posts: 16
Joined: Fri Mar 04, 2005 4:45 am

Post by Cep »

Ok that will allow for the first record but then after next nothing happens. I still think something is not quite right, I just can't figure out what it is.
Post Reply