• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 472
  • Last Modified:

foreach -to- while

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
Passionate
Asked:
Passionate
  • 3
  • 2
2 Solutions
 
lozlozCommented:
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
 
PassionateAuthor Commented:
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
 
lozlozCommented:
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
 
PassionateAuthor Commented:
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
 
lozlozCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now