foreach and multi-record forms

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
redtux
Forum Newbie
Posts: 7
Joined: Thu Mar 04, 2004 8:40 am

foreach and multi-record forms

Post by redtux »

I am trying to do the following

Pull out a range of data from a pg table
This works

Then put it into a multi record html form

this is my current code

Code: Select all

<html>
<body>
<?php

$db = pg_connect("dbname=data_cc");
$query = "SELECT * FROM tb_contacts WHERE contact_id<'41'";
$result = pg_exec($db, $query);
if (!$result) {
printf ("ERROR");
$errormessage = pg_errormessage($db);
echo $errormessage;
exit;
}
$numrows = pg_numrows($result);
$row=0;
$myrow = pg_fetch_row ($result,$row);

foreach ($myrow as $value){
printf ("$row");
echo('<form name="contacts" method="POST" action="update1.php">');
echo('<table>');
echo('<tr>');
echo('<td>');
echo('<input type="text" name="id'. $row .'" value="' .$myrow[0] .'">');
echo('<input type="text" name="first name'. $row .'" value="' .$myrow[2] .'">');
echo('</td>');
echo('</tr>');
echo('<tr>');
echo('<td>');
}
?>
<input type='submit' value='Save to Cart'>
</td>
</tr>
</table>

</form>



}
?>
</body>
</html>
However it only brings the last record repeted several times

Any help much appreciated
User avatar
liljester
Forum Contributor
Posts: 400
Joined: Tue May 20, 2003 4:49 pm

Post by liljester »

you're not looping through all the results, you're only reading one result:
$myrow = pg_fetch_row ($result,$row);

here is a suggestion:

Code: Select all

$db = pg_connect("dbname=data_cc"); 
$query = "SELECT * FROM tb_contacts WHERE contact_id<'41'"; 
$result = pg_exec($db, $query); 
if (!$result) { 
    printf ("ERROR"); 
    $errormessage = pg_errormessage($db); 
    echo $errormessage; 
    exit; 
}

$numrows = pg_numrows($result); 
$row=0; 

// in the function pg_fetch_row($result, $row), $row is optional after php version 4.1, if you leave it out, it automaticly increments
while($myrow = pg_fetch_row ($result)){
    $row++;
    printf ("$row"); 
    echo('<form name="contacts" method="POST" action="update1.php">'); 
    echo('<table>'); 
    echo('<tr>'); 
    echo('<td>'); 
    echo('<input type="text" name="id'. $row .'" value="' .$myrow[0] .'">'); 
    echo('<input type="text" name="first name'. $row .'" value="' .$myrow[2] .'">'); 
    echo('</td>'); 
    echo('</tr>'); 
    echo('<tr>'); 
    echo('<td>'); 
}
redtux
Forum Newbie
Posts: 7
Joined: Thu Mar 04, 2004 8:40 am

Boolean type

Post by redtux »

Thanks that worked, now for the next question I have the following query scrap

Code: Select all

$query = "UPDATE tb_contacts SET first_name=
CASE WHEN contact_id='$id1' THEN first_name="$firstname1" END";
$result = pg_exec($db, $query);
if (!$result) &#123;
printf ("ERROR");
$errormessage = pg_errormessage($db);
echo $errormessage;
exit;
&#125;
The problem is that when I try to run the query within a CASE statement the $firstname string is being read as a boolean type

anyone any ideas
Post Reply