foreach woes

I'm trying to extend the script(s) provided at
www.phpriot.com/d/articles/client-side/sortable-lists-with-p hp-and-ajax/.
I'm struggling with the foreach syntax - I guess I'm just a dummy. I
still don't get it despite reading through http://uk.php.net/foreach.

Basically, there's a movie table (`movie_id`, `title`, `description`,
`ranking`).

and a function that gets the movies:

function getMovies()
{
$query = 'select movie_id, title, description from movies order
by ranking, lower(title)';
$result = mysql_query($query);

$movies = array();
while ($row = mysql_fetch_object($result)) {
$movies[$row->movie_id] = $row->title;
}

return $movies;
}


and here's where the titles are displayed on the page:

<ul id="movies_list" class="sortable-list">
<?php foreach ($movies as $movie_id => $title) { ?>
<li id="movie_<?= $movie_id ?>"><?= $title ?></li>
<?php } ?>
</ul>

For each movie what do I need to do to echo the movie description -
within the <li> statement for instance.

Hope that makes sense. Any help appreciated.
zac.carey [ Mi, 07 Juni 2006 19:06 ] [ ID #1346141 ]

Re: foreach woes

strawberry wrote:
> I'm trying to extend the script(s) provided at
> www.phpriot.com/d/articles/client-side/sortable-lists-with-p hp-and-ajax/.
> I'm struggling with the foreach syntax - I guess I'm just a dummy. I
> still don't get it despite reading through http://uk.php.net/foreach.
>
> Basically, there's a movie table (`movie_id`, `title`, `description`,
> `ranking`).
>
> and a function that gets the movies:
>
> function getMovies()
> {
> $query = 'select movie_id, title, description from movies order
> by ranking, lower(title)';
> $result = mysql_query($query);
>
> $movies = array();
> while ($row = mysql_fetch_object($result)) {
> $movies[$row->movie_id] = $row->title;
> }
>
> return $movies;
> }
>
>
> and here's where the titles are displayed on the page:
>
> <ul id="movies_list" class="sortable-list">
> <?php foreach ($movies as $movie_id => $title) { ?>
> <li id="movie_<?= $movie_id ?>"><?= $title ?></li>
> <?php } ?>
> </ul>
>
> For each movie what do I need to do to echo the movie description -
> within the <li> statement for instance.
>
> Hope that makes sense. Any help appreciated.

In the getMovies() function, change the while loop to something like
this:

while ($row = mysql_fetch_object($result)) {
$movies[] = $row;
}

And when you display them:

foreach($movies as $movie) {
//$movie->movie_id is the movie_id
//$movie->title is the title
//$movie->description is the description
}
zeldorblat [ Mi, 07 Juni 2006 19:33 ] [ ID #1346143 ]

Re: foreach woes

Perfect. Thank you very much.

ZeldorBlat wrote:
> strawberry wrote:
> > I'm trying to extend the script(s) provided at
> > www.phpriot.com/d/articles/client-side/sortable-lists-with-p hp-and-ajax/.
> > I'm struggling with the foreach syntax - I guess I'm just a dummy. I
> > still don't get it despite reading through http://uk.php.net/foreach.
> >
> > Basically, there's a movie table (`movie_id`, `title`, `description`,
> > `ranking`).
> >
> > and a function that gets the movies:
> >
> > function getMovies()
> > {
> > $query = 'select movie_id, title, description from movies order
> > by ranking, lower(title)';
> > $result = mysql_query($query);
> >
> > $movies = array();
> > while ($row = mysql_fetch_object($result)) {
> > $movies[$row->movie_id] = $row->title;
> > }
> >
> > return $movies;
> > }
> >
> >
> > and here's where the titles are displayed on the page:
> >
> > <ul id="movies_list" class="sortable-list">
> > <?php foreach ($movies as $movie_id => $title) { ?>
> > <li id="movie_<?= $movie_id ?>"><?= $title ?></li>
> > <?php } ?>
> > </ul>
> >
> > For each movie what do I need to do to echo the movie description -
> > within the <li> statement for instance.
> >
> > Hope that makes sense. Any help appreciated.
>
> In the getMovies() function, change the while loop to something like
> this:
>
> while ($row = mysql_fetch_object($result)) {
> $movies[] = $row;
> }
>
> And when you display them:
>
> foreach($movies as $movie) {
> //$movie->movie_id is the movie_id
> //$movie->title is the title
> //$movie->description is the description
> }
zac.carey [ Mi, 07 Juni 2006 19:56 ] [ ID #1346146 ]

Re: foreach woes

Curiously, the last movie (movie_id 7) now always appears at the bottom
of the ranked list. Have I done something wrong or is my computer just
exercising some form of editorial control - after all, it is a Nicholas
Cage movie!?!

The rank order processing function looks like this:

function processMoviesOrder($key)
{
if (!isset($_POST[$key]) || !is_array($_POST[$key]))
return;

$movies = getMovies();
$queries = array();
$ranking = 1;

foreach ($_POST[$key] as $movie_id) {
if (!array_key_exists($movie_id, $movies))
continue;

$query = sprintf('update movies set ranking = %d where movie_id =
%d',
$ranking,
$movie_id);

mysql_query($query);
$ranking++;
}

and the complete presentation page now looks like this;

<?php
require_once('database.php');
require_once('movies.php');

if (!dbConnect()) {
echo 'Error connecting to database';
exit;
}

$movies = getMovies();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"DTD/xhtml1-strict.dtd">
<html>
<head>
<title>phpRiot Sortable Lists</title>
<link rel="stylesheet" type="text/css" href="styles.css" />

<script type="text/javascript"
src="../javascripts/prototype.js"></script>
<script type="text/javascript"
src="../javascripts/scriptaculous.js"></script>
</head>
<body>
<h1>phpRiot Sortable Lists</h1>

<ul id="movies_list" class="sortable-list">
<?php

foreach($movies as $movie) {
//$movie->movie_id is the movie_id
//$movie->title is the title
//$movie->description is the description
?>
<li id="movie_<?= $movie->movie_id ?>"><?=
$movie->title.$movie->description ?></li>
<?php } ?>
</ul>

<script type="text/javascript">
function updateOrder()
{
var options = {
method : 'post',
parameters :
Sortable.serialize('movies_list')
};

new Ajax.Request('processor.php', options);
}

Sortable.create('movies_list', { onUpdate : updateOrder });
</script>
</body>
</html>

the javascripts referred to come from the scriptaculous website

strawberry wrote:
> Perfect. Thank you very much.
>
> ZeldorBlat wrote:
> > strawberry wrote:
> > > I'm trying to extend the script(s) provided at
> > > www.phpriot.com/d/articles/client-side/sortable-lists-with-p hp-and-ajax/.
> > > I'm struggling with the foreach syntax - I guess I'm just a dummy. I
> > > still don't get it despite reading through http://uk.php.net/foreach.
> > >
> > > Basically, there's a movie table (`movie_id`, `title`, `description`,
> > > `ranking`).
> > >
> > > and a function that gets the movies:
> > >
> > > function getMovies()
> > > {
> > > $query = 'select movie_id, title, description from movies order
> > > by ranking, lower(title)';
> > > $result = mysql_query($query);
> > >
> > > $movies = array();
> > > while ($row = mysql_fetch_object($result)) {
> > > $movies[$row->movie_id] = $row->title;
> > > }
> > >
> > > return $movies;
> > > }
> > >
> > >
> > > and here's where the titles are displayed on the page:
> > >
> > > <ul id="movies_list" class="sortable-list">
> > > <?php foreach ($movies as $movie_id => $title) { ?>
> > > <li id="movie_<?= $movie_id ?>"><?= $title ?></li>
> > > <?php } ?>
> > > </ul>
> > >
> > > For each movie what do I need to do to echo the movie description -
> > > within the <li> statement for instance.
> > >
> > > Hope that makes sense. Any help appreciated.
> >
> > In the getMovies() function, change the while loop to something like
> > this:
> >
> > while ($row = mysql_fetch_object($result)) {
> > $movies[] = $row;
> > }
> >
> > And when you display them:
> >
> > foreach($movies as $movie) {
> > //$movie->movie_id is the movie_id
> > //$movie->title is the title
> > //$movie->description is the description
> > }
zac.carey [ Do, 08 Juni 2006 14:39 ] [ ID #1347924 ]

Re: foreach woes

strawberry wrote:
> Curiously, the last movie (movie_id 7) now always appears at the bottom
> of the ranked list. Have I done something wrong or is my computer just
> exercising some form of editorial control - after all, it is a Nicholas
> Cage movie!?!
>
> The rank order processing function looks like this:
>
> function processMoviesOrder($key)
> {
> if (!isset($_POST[$key]) || !is_array($_POST[$key]))
> return;
>
> $movies = getMovies();
> $queries = array();
> $ranking = 1;
>
> foreach ($_POST[$key] as $movie_id) {
> if (!array_key_exists($movie_id, $movies))
> continue;
>
> $query = sprintf('update movies set ranking = %d where movie_id =
> %d',
> $ranking,
> $movie_id);
>
> mysql_query($query);
> $ranking++;
> }
>
> and the complete presentation page now looks like this;
>
> <?php
> require_once('database.php');
> require_once('movies.php');
>
> if (!dbConnect()) {
> echo 'Error connecting to database';
> exit;
> }
>
> $movies = getMovies();
> ?>
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
> "DTD/xhtml1-strict.dtd">
> <html>
> <head>
> <title>phpRiot Sortable Lists</title>
> <link rel="stylesheet" type="text/css" href="styles.css" />
>
> <script type="text/javascript"
> src="../javascripts/prototype.js"></script>
> <script type="text/javascript"
> src="../javascripts/scriptaculous.js"></script>
> </head>
> <body>
> <h1>phpRiot Sortable Lists</h1>
>
> <ul id="movies_list" class="sortable-list">
> <?php
>
> foreach($movies as $movie) {
> //$movie->movie_id is the movie_id
> //$movie->title is the title
> //$movie->description is the description
> ?>
> <li id="movie_<?= $movie->movie_id ?>"><?=
> $movie->title.$movie->description ?></li>
> <?php } ?>
> </ul>
>
> <script type="text/javascript">
> function updateOrder()
> {
> var options = {
> method : 'post',
> parameters :
> Sortable.serialize('movies_list')
> };
>
> new Ajax.Request('processor.php', options);
> }
>
> Sortable.create('movies_list', { onUpdate : updateOrder });
> </script>
> </body>
> </html>
>
> the javascripts referred to come from the scriptaculous website
>
> strawberry wrote:
> > Perfect. Thank you very much.
> >
> > ZeldorBlat wrote:
> > > strawberry wrote:
> > > > I'm trying to extend the script(s) provided at
> > > > www.phpriot.com/d/articles/client-side/sortable-lists-with-p hp-and-ajax/.
> > > > I'm struggling with the foreach syntax - I guess I'm just a dummy. I
> > > > still don't get it despite reading through http://uk.php.net/foreach.
> > > >
> > > > Basically, there's a movie table (`movie_id`, `title`, `description`,
> > > > `ranking`).
> > > >
> > > > and a function that gets the movies:
> > > >
> > > > function getMovies()
> > > > {
> > > > $query = 'select movie_id, title, description from movies order
> > > > by ranking, lower(title)';
> > > > $result = mysql_query($query);
> > > >
> > > > $movies = array();
> > > > while ($row = mysql_fetch_object($result)) {
> > > > $movies[$row->movie_id] = $row->title;
> > > > }
> > > >
> > > > return $movies;
> > > > }
> > > >
> > > >
> > > > and here's where the titles are displayed on the page:
> > > >
> > > > <ul id="movies_list" class="sortable-list">
> > > > <?php foreach ($movies as $movie_id => $title) { ?>
> > > > <li id="movie_<?= $movie_id ?>"><?= $title ?></li>
> > > > <?php } ?>
> > > > </ul>
> > > >
> > > > For each movie what do I need to do to echo the movie description -
> > > > within the <li> statement for instance.
> > > >
> > > > Hope that makes sense. Any help appreciated.
> > >
> > > In the getMovies() function, change the while loop to something like
> > > this:
> > >
> > > while ($row = mysql_fetch_object($result)) {
> > > $movies[] = $row;
> > > }
> > >
> > > And when you display them:
> > >
> > > foreach($movies as $movie) {
> > > //$movie->movie_id is the movie_id
> > > //$movie->title is the title
> > > //$movie->description is the description
> > > }

Look at your new version of getMovies() and consider what the keys of
the array are.
zeldorblat [ Do, 08 Juni 2006 19:40 ] [ ID #1347926 ]

Re: foreach woes

Thanks,

But I'm going to have to have a bit of a think about that. I may be
some time...

I'll let you know when I've figured it out.

Cheers :-)


ZeldorBlat wrote:
> strawberry wrote:
> > Curiously, the last movie (movie_id 7) now always appears at the bottom
> > of the ranked list. Have I done something wrong or is my computer just
> > exercising some form of editorial control - after all, it is a Nicholas
> > Cage movie!?!
> >
> > The rank order processing function looks like this:
> >
> > function processMoviesOrder($key)
> > {
> > if (!isset($_POST[$key]) || !is_array($_POST[$key]))
> > return;
> >
> > $movies = getMovies();
> > $queries = array();
> > $ranking = 1;
> >
> > foreach ($_POST[$key] as $movie_id) {
> > if (!array_key_exists($movie_id, $movies))
> > continue;
> >
> > $query = sprintf('update movies set ranking = %d where movie_id =
> > %d',
> > $ranking,
> > $movie_id);
> >
> > mysql_query($query);
> > $ranking++;
> > }
> >
> > and the complete presentation page now looks like this;
> >
> > <?php
> > require_once('database.php');
> > require_once('movies.php');
> >
> > if (!dbConnect()) {
> > echo 'Error connecting to database';
> > exit;
> > }
> >
> > $movies = getMovies();
> > ?>
> > <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
> > "DTD/xhtml1-strict.dtd">
> > <html>
> > <head>
> > <title>phpRiot Sortable Lists</title>
> > <link rel="stylesheet" type="text/css" href="styles.css" />
> >
> > <script type="text/javascript"
> > src="../javascripts/prototype.js"></script>
> > <script type="text/javascript"
> > src="../javascripts/scriptaculous.js"></script>
> > </head>
> > <body>
> > <h1>phpRiot Sortable Lists</h1>
> >
> > <ul id="movies_list" class="sortable-list">
> > <?php
> >
> > foreach($movies as $movie) {
> > //$movie->movie_id is the movie_id
> > //$movie->title is the title
> > //$movie->description is the description
> > ?>
> > <li id="movie_<?= $movie->movie_id ?>"><?=
> > $movie->title.$movie->description ?></li>
> > <?php } ?>
> > </ul>
> >
> > <script type="text/javascript">
> > function updateOrder()
> > {
> > var options = {
> > method : 'post',
> > parameters :
> > Sortable.serialize('movies_list')
> > };
> >
> > new Ajax.Request('processor.php', options);
> > }
> >
> > Sortable.create('movies_list', { onUpdate : updateOrder });
> > </script>
> > </body>
> > </html>
> >
> > the javascripts referred to come from the scriptaculous website
> >
> > strawberry wrote:
> > > Perfect. Thank you very much.
> > >
> > > ZeldorBlat wrote:
> > > > strawberry wrote:
> > > > > I'm trying to extend the script(s) provided at
> > > > > www.phpriot.com/d/articles/client-side/sortable-lists-with-p hp-and-ajax/.
> > > > > I'm struggling with the foreach syntax - I guess I'm just a dummy. I
> > > > > still don't get it despite reading through http://uk.php.net/foreach.
> > > > >
> > > > > Basically, there's a movie table (`movie_id`, `title`, `description`,
> > > > > `ranking`).
> > > > >
> > > > > and a function that gets the movies:
> > > > >
> > > > > function getMovies()
> > > > > {
> > > > > $query = 'select movie_id, title, description from movies order
> > > > > by ranking, lower(title)';
> > > > > $result = mysql_query($query);
> > > > >
> > > > > $movies = array();
> > > > > while ($row = mysql_fetch_object($result)) {
> > > > > $movies[$row->movie_id] = $row->title;
> > > > > }
> > > > >
> > > > > return $movies;
> > > > > }
> > > > >
> > > > >
> > > > > and here's where the titles are displayed on the page:
> > > > >
> > > > > <ul id="movies_list" class="sortable-list">
> > > > > <?php foreach ($movies as $movie_id => $title) { ?>
> > > > > <li id="movie_<?= $movie_id ?>"><?= $title ?></li>
> > > > > <?php } ?>
> > > > > </ul>
> > > > >
> > > > > For each movie what do I need to do to echo the movie description -
> > > > > within the <li> statement for instance.
> > > > >
> > > > > Hope that makes sense. Any help appreciated.
> > > >
> > > > In the getMovies() function, change the while loop to something like
> > > > this:
> > > >
> > > > while ($row = mysql_fetch_object($result)) {
> > > > $movies[] = $row;
> > > > }
> > > >
> > > > And when you display them:
> > > >
> > > > foreach($movies as $movie) {
> > > > //$movie->movie_id is the movie_id
> > > > //$movie->title is the title
> > > > //$movie->description is the description
> > > > }
>
> Look at your new version of getMovies() and consider what the keys of
> the array are.
zac.carey [ Fr, 09 Juni 2006 13:05 ] [ ID #1349630 ]
PHP » alt.php.sql » foreach woes

Vorheriges Thema: advanced search through table
Nächstes Thema: Mysql question