Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

foreach -to- while

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article discusses how to implement server side field validation and display customized error messages to the client.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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.

704 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