Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

foreach -to- while

Posted on 2003-11-10
5
Medium Priority
?
448 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 375 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 375 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

Industry Leaders: 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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

926 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