Page 1 of 1

Need php/mysql help -- I think I need a "foreach" -- how to do it?

Posted: Mon Nov 12, 2007 10:06 pm
by SineMacula
I'm creating a script that will query the database and present a list of records for the logged in user. I want the user to be able to click on a record to go to a full report for that particular instance. So far I can't get it working - no matter what record you click, the last record is the one that gets displayed in the report.

Here's what I've got (using includePHP plugin to do it in an article, since making a component is beyond my ability and I don't have time to figure it out):

Code: Select all

 {php}

$db = JFactory::getDBO();

$user = &JFactory::getUser();

$sqlout = "SELECT * FROM `jos_chronoforms_3` WHERE `username` ='$user->username'";

$db->setQuery($sqlout);

$resultout = mysql_query($sqlout);

while ($myrow = mysql_fetch_assoc($resultout)) {

?>


Name: <?php echo $user->name; ?><br />
Date: <?php echo $myrow['recordtime']; ?><br />
Topic 1: <?php echo $myrow['topic1']; ?><br /><br />
<form name="getreport" action="index.php?option=com_content&view=article&id=47&Itemid=58" method="post">
<input type="textbox" name="recorded" value="<?php echo $myrow['recordtime']; ?>">
<input type="submit" value="Get Report"><br /><br />
<?php
}
?>
{/php}


So, what's supposed to happen is you get a list of all of your submissions, with a "Get Report" button for each one. Clicking the submit button should take you to a page with the full report for that record (by going to another php script in an article that uses the $_POST['recorded'] value to get the record). However, no matter what submit button is clicked, it always brings up the the report for the last record.

I think I probably need some kind of foreach statement so that the value of "recorded" that gets passed is the right one.

I made the input type a textbox just so I could verify it was pulling the right 'recordtime' values for the records (I had it hidden) - and that's fine.

Suggestions?

Re: Need php/mysql help -- I think I need a "foreach" -- how to do it?

Posted: Tue Nov 13, 2007 1:39 am
by lobos
$db = JFactory::getDBO();
$user = &JFactory::getUser();
$sqlout = "SELECT * FROM `jos_chronoforms_3` WHERE `username` ='$user->username'";

$db->setQuery($sqlout);

$rows = $db->loadObjectList();

foreach ( $rows as $row ){
?>
Name: name; ?>

Date: recordtime; ?>

Topic 1: topic1; ?>







}
?>


I haven't tested this, but it should work...

-Lobos

Re: Need php/mysql help -- I think I need a "foreach" -- how to do it?

Posted: Tue Nov 13, 2007 1:48 am
by lobos
ps: don't forget to close () that
tag...

Re: Need php/mysql help -- I think I need a "foreach" -- how to do it?

Posted: Tue Nov 13, 2007 2:06 am
by SineMacula
Thanks, lobos!!

As it turns out, my original code works too... once I added the closing form tag.  :-[

Re: Need php/mysql help -- I think I need a "foreach" -- how to do it?

Posted: Sun Nov 25, 2007 6:28 am
by Babysittah
Hi y'all, i just dropped in to check what ur talkin about here and i found some useful info 4 me, thanx:)