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

Discussion and education for beginner / novice programmers interested in embarking on the development process to take advantage of the extensible nature of the Joomla! CMS.

Moderators: tjay, seadap, Rogue4ngel, matthewhayashida

Forum rules
Post Reply
User avatar
SineMacula
Joomla! Intern
Joomla! Intern
Posts: 77
Joined: Sun Aug 21, 2005 5:16 am
Location: San Jose, CA
Contact:

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

Post by SineMacula » Mon Nov 12, 2007 10:06 pm

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?

User avatar
lobos
Joomla! Apprentice
Joomla! Apprentice
Posts: 30
Joined: Wed Jul 19, 2006 3:33 pm
Location: Sao Paulo, Brasil
Contact:

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

Post by lobos » Tue Nov 13, 2007 1:39 am

$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
Respect and honour, sempre isso, the tribal way, the only way.
http://en.wikipedia.org/wiki/Tribes_Vengeance

User avatar
lobos
Joomla! Apprentice
Joomla! Apprentice
Posts: 30
Joined: Wed Jul 19, 2006 3:33 pm
Location: Sao Paulo, Brasil
Contact:

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

Post by lobos » Tue Nov 13, 2007 1:48 am

ps: don't forget to close () that
tag...
Respect and honour, sempre isso, the tribal way, the only way.
http://en.wikipedia.org/wiki/Tribes_Vengeance

User avatar
SineMacula
Joomla! Intern
Joomla! Intern
Posts: 77
Joined: Sun Aug 21, 2005 5:16 am
Location: San Jose, CA
Contact:

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

Post by SineMacula » Tue Nov 13, 2007 2:06 am

Thanks, lobos!!

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

User avatar
Babysittah
Joomla! Fledgling
Joomla! Fledgling
Posts: 2
Joined: Sun Nov 25, 2007 6:13 am

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

Post by Babysittah » Sun Nov 25, 2007 6:28 am

Hi y'all, i just dropped in to check what ur talkin about here and i found some useful info 4 me, thanx:)
http://www.newbabyassistant.com - newborn care monitoring and documenting service. Nurition, bathing, exercises, outdoor activities, physiological parameters.


Post Reply