Solved

foreach -to- while

Posted on 2003-11-10
5
437 Views
Last Modified: 2006-11-17
I need to covert a threaded (recursive) comment system from PHP/MySQL -to- PHP/ADOdb. However, I am having minor problems with using foreach() loops instead of while() loops, because ADOdb has no "fetch array" functions per say.

Instead of "posting" all of the code:
http://hotwired.lycos.com/webmonkey/99/31/stuff3a/functions.html
http://hotwired.lycos.com/webmonkey/99/31/stuff3a/display_topic.html
http://hotwired.lycos.com/webmonkey/99/31/stuff3a/write_topic.html

"Any help at all" would be greatly appreciated!
0
Comment
Question by:Passionate
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 13

Expert Comment

by:lozloz
ID: 9719781
could you be a bit more specific as to which while you can't do? and if it has no fetch array, how are you loading the data into your script from the database?

loz
0
 

Author Comment

by:Passionate
ID: 9722376
Ok, quick example

***********************************************

PHP/MySQL

***********************************************
<?php
      $query = 'SELECT var, var2 FROM table';
      $result = mysql_query($query) or die(mysql_error());
      while($row = mysql_fetch_assoc($result)) {
            extract($row);
            echo $var . '<BR />' . $var2;
      }
?>
***********************************************

PHP/ADOdb

***********************************************
<?php
      $query = 'SELECT var, var2 FROM table';
      $results = $db->GetAll($query) or die($db->ErrorMsg());
      foreach($results AS $row) {
            extract($row);
            echo $var . '<BR />' . $var2;
      }
?>
***********************************************

Now look at the first script here
ttp://hotwired.lycos.com/webmonkey/99/31/stuff3a/functions.html

I need to convert the while/list() loops to foreach()
0
 
LVL 13

Assisted Solution

by:lozloz
lozloz earned 125 total points
ID: 9722529
hi,

<?
function DisplayKids ($topic_id=0, $level=0)
{
      $comments = 0;
      $query = "select topic_id, name, author "
            .", create_dt, description from px_topics where "
            ." parent_id = $topic_id "
            ." order by create_dt, topic_id "
      ;
#print "<p>$query</p>\n";
              $result = $db->GetAll($query) or die($db->ErrorMsg());
              foreach($result AS $row) {
                list($r_topic_id, $r_name, $r_author, $r_create_dt, $r_description) = $row;
            if (!$level)
            {
                  if (!$comments)
                  {
                        print "<b>Comments:</b><br>\n";
                  }
                  print "<table border=0>\n"
                  ."<tr bgcolor=skyblue><td colspan=2 width=500>\n";
            }
            else
            {
                  if (!$comments)
                  {
                        print "<ul>\n";
                  }
                  print "<li>";
            }
            $comments++;
            if (!eregi("[a-z0-9]",$r_author)) { $r_author = "[no name]"; }
            if ($r_topic_id != $topic_id)
            {
?>
<a href="display_topic.phtml?topic_id=<? print $r_topic_id; ?>"><? print $r_name; ?></a> by <b><? print $r_author; ?></b>
<?
            }
            else
            {
?>
<? print $r_name; ?> by <b><? print $r_author; ?></b>
<?
                     }  
?>
( <? print $r_topic_id; ?> )
<?
            if ($level)
            {
                  print "<br>\n";
            }
            else
            {
                  print "</td></tr>\n"
                        ."<tr><td width=2> </td>\n"
                        ."<td width=498>$r_description</td>\n"
                        ."</tr></table><br>\n"
                  ;
            }
            DisplayKids($r_topic_id, $level+1);
      }
      if ($level && $comments)
      {
            print "</ul>\n";
      }
}

function DisplayTopic ($topic_id=0, $level=0, $nokids=0)
{

      $fields = array("topic_id", "parent_id", "root_id", "name"
            , "description", "author", "author_host", "create_dt"
            , "modify_dt"
      );
      $query = "select distinct ";
      $comma = "";
      foreach($fields AS $key => $val)
      {
            $query .= $comma."t.".$val;
            $comma = ", ";
      }
      if (!$topic_id)
      {
            $query .= " from px_topics t , px_topics r "
                  ."where t.topic_id = r.root_id "
            ;
#print "<p>$query</p>\n";
            $result = $db->GetAll($query) or die($db->ErrorMsg());
            if (!$result)
            {
                  print "<p>Damn! result = $result</p>\n";
            }
            else
            {
                  foreach($result AS $row) {
                  {
                        list($topic_id, $parent_id, $root_id, $name
                              , $description , $author, $author_host
                              , $create_dt, $modify_dt, $parent_name
                              , $root_name
                        ) = $row;
                        print "<p>"
                        ."<a href=\"display_topic.phtml?topic_id="
                              .$topic_id
                              ."\">"
                              .$name
                              ."</a></p>\n"
                        ;
                  }
            }
            return array(0,0,"");
      }
      else
      {
            $query .= ",p.name,r.name "
                  ."from px_topics t "
                  ."left join px_topics as p on t.parent_id = p.topic_id "
                  ."left join px_topics as r on t.root_id = r.topic_id "
                  ."where t.topic_id = $topic_id "
            ;
#print "<p>$query</p>\n";
            $result = $db->GetAll($query) or die($db->ErrorMsg());
            if (!$result)
            {
                  print "<p>Damn! result = $result</p>\n";
            }
            else
            {
                  list($topic_id, $parent_id, $root_id, $name
                        , $description , $author, $author_host
                        , $create_dt, $modify_dt, $parent_name
                        , $root_name
                  ) = $row;
            }
      }

      if ($author == "") { $author = "[no name]"; }

      if ($root_id != $topic_id && $root_id != $parent_id)
      {
            print "<p>\n<b>root:</b>\n"
                  ."<a href=\"display_topic.phtml?topic_id="
                  .$root_id
                  ."\">"
                  .$root_name
                  ."</a>\n</p>\n"
            ;
      }
      if (isset($parent_name) && $parent_name > "")
      {
            print "<p>\n<b>parent:</b>\n"
                  ."<a href=\"display_topic.phtml?topic_id="
                  .$parent_id
                  ."\">"
                  .$parent_name
                  ."</a>\n</p>\n"
            ;
      }
      print "<p>\n"
            ."<b>$name</b> by <b>$author</b> ($author_host) "
            ."on <b>$create_dt</b>\n"
            ."</p>\n"
            ."<p>\n"
            .$description
            ."</p>\n"
      ;

      if (!$nokids)
      {
            print "<p><b>"
                  ."<a href=\"write_topic.phtml?topic_id=$topic_id\">"
                  ."Reply to this</a></b></p>\n"
            ;
            print "<p>\n";
            DisplayKids($topic_id, $level);
            print "</p>\n";
      }
      return array($root_id, $parent_id, $name);
}

function FixQuotes ($what = "")
{
      $what = ereg_replace("'","''",$what);
      $counter = 0;
      while (eregi("\\\\'", $what) && $counter < 10)
      {
            $what = ereg_replace("\\\\'","'",$what);
#print "<p>counter=[$counter] what=[$what]</p>\n";
      }
      return $what;
}

function CreateTopic ($name="[no name]", $author="[no author]"
      , $description="[no comments]", $parent_id=0, $root_id=0
)
{
      $name = FixQuotes($name);
      $description = FixQuotes($description);
      $author = FixQuotes($author);
      $query = "insert into px_topics "
            ." (name,description, parent_id, root_id "
            ." , author, author_host) "
            ." values ('$name', '$description', $parent_id "
            ." , $root_id, '$author', '".getenv("REMOTE_ADDR")."' "
            .") "
      ;
print "<p>$query</p>\n";
      $result = $db->GetAll($query) or die($db->ErrorMsg());
      if (!$result)
      {
            print "<p>hey! insert failed, dammit.</p>\n";
      }
      $result = $db->GetAll("select last_insert_id()") or die($db->ErrorMsg());
      list($topic_id) = $result;

      if (isset($topic_id) && $topic_id > 0)
      {
#print "<p>topic_id = [$topic_id]</p>\n";
            if ($root_id == 0)
            {
                  $root_id = $topic_id;
                  $db->GetAll("update px_topics set root_id=$topic_id "
                        ." where topic_id = $topic_id and root_id=0 "
                  ) or die($db->ErrorMsg());
            }
      }
      else
      {
            print "<p><b>Hey! that didn't work.</p>\n";
      }
      return $topic_id;
}
?>

tell me if that works for you

loz
0
 

Author Comment

by:Passionate
ID: 9727182
Thank you lozloz, but I am afraid that did NOT work, no errors - just a blank page. Note when you INSERT or UPDATE in ADOdb use the Execute() function rather than the GetAll() function which is for a SELECT.
0
 
LVL 13

Accepted Solution

by:
lozloz earned 125 total points
ID: 9727566
i assume that you replaced the getall's for execute where appropriate and it still didn't work then? put this at the top of the script: error_reporting (E_ALL); and try running it again

loz
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Session time out quickly. 6 55
MySQL-Design Help 12 67
How to add month values to a drop down menu that can be sorted by month from MYSQL 10 53
Conditional Array Element 7 28
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question