The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> PHP FOR loops and MySQL table rows
Christian J
post Jun 28 2010, 03:08 PM
Post #1


.
********

Group: WDG Moderators
Posts: 4,741
Joined: 10-August 06
Member No.: 7



Just for practice I'm trying to loop through a MySQL table, comparing each row with the rest. With (multidimensional) arrays I might do this with nested FOR loops --doesn't that work with MySQL tables? Haven't found much on the subject, but http://www.php.net/manual/en/function.mysq...-rows.php#47520 says that "there seems no way of getting the CURRENT row number that's under iteration in a typical loop". If true, this seems very limiting... unsure.gif
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Jun 28 2010, 04:49 PM
Post #2


.
********

Group: WDG Moderators
Posts: 4,741
Joined: 10-August 06
Member No.: 7



Seems you can at least make your own PHP array:

CODE
$query = mysql_query("SELECT * FROM foo");

// make PHP array
$i=0;
while($item = mysql_fetch_array($query))
{
    $arr[$i]=$item;
    $i++;
}

// print PHP array
echo '<pre>';
for($j=0; $j<count($arr); $j++)
{
    echo "row $j: ";
    for($n=0; $n<count($arr[$j]); $n++)
    {
        echo $arr[$j][$n]." ";
    }
    echo "\n";
}
echo '</pre>';
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Jun 28 2010, 05:14 PM
Post #3


.
********

Group: WDG Moderators
Posts: 4,741
Joined: 10-August 06
Member No.: 7



For SQLite PHP offers sqlite_array_query(), which returns an array --not a resource-- so you can apply FOR loops right away. Strange that such a function doesn't exist for MySQL.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Brian Chandler
post Jun 28 2010, 10:32 PM
Post #4


Jocular coder
********

Group: Members
Posts: 2,129
Joined: 31-August 06
Member No.: 43



QUOTE(Christian J @ Jun 29 2010, 07:14 AM) *

For SQLite PHP offers sqlite_array_query(), which returns an array --not a resource-- so you can apply FOR loops right away. Strange that such a function doesn't exist for MySQL.


Well, then write your own function. I don't quite understand the problem here: generally you get rows from mysql using a while loop, and if you want to count the row number, for example to add a heading every ten rows, then it's something like:

CODE

$row = 0;
while ($row = mysql_fetch_array(...))
{  if ($row%10 == 0)
      <... show header line...>

  ... show stuff ...

   $row++;
}


The thing that did seem to be missing from mysql is "Get rows 11-20 of query", which is what you need for output paging. But you can just write your own function to do what you need. (This is the difference between programming, where things get easier as you go along, and point-and-grunt manual stuff, like using Whirred or CSS, where they get more and more tangled up.)


--------------------
Brian Chandler
Nothing in this post constitutes "commercial solicitation". PayPal does not solicit residents of Japan. Contents may settle in transit. "Legal mind" may or may not be brain-damaged.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 18th May 2013 - 11:44 PM