Solved

foreach -to- while

Posted on 2003-11-10
5
432 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
  • 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how can i gnupg encrypt a PDF document? 12 33
website maintenance mode 1 17
Decrypt string by php 7 32
What does != "" mean in programming 8 35
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

810 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