SELECT REPEAT statement - how do you do it?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

SELECT REPEAT statement - how do you do it?

Post by simonmlewis »

We need to run a tickertape, and it needs to be from a database.
Once x amount of results have been shown, we want it to start over. So either in a loop, or from what I read, using REPEAT.

But I cannot see how to code the REPEAT statement. ie. telling it how many times, or infinite.

Can anyone please help.

Thanks.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: SELECT REPEAT statement - how do you do it?

Post by Celauran »

I don't see where/why REPEAT would be needed for this application. Pull down the articles you want to display and hand them off to whatever is going to be handling your ticker tape. It can handle looping the content.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: SELECT REPEAT statement - how do you do it?

Post by simonmlewis »

It would be a simple database query that echoes the <a href='#'>here</a>&nbsp;|&nbsp<a href='#'>here</a>&nbsp;|&nbsp..... but if it finds 5, it will show 5... then stop.
I want that to loop.

Unless you are suggesting I wrap the whole query in a loop? So do a :

Code: Select all

$count = 0;
while ($count < 100)
{
$result = ......
}
??

I just thought using REPEAT would be more efficient?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: SELECT REPEAT statement - how do you do it?

Post by Celauran »

I thought you wanted the same five items to repeat. You could accomplish that with a single query.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: SELECT REPEAT statement - how do you do it?

Post by simonmlewis »

How?
Each five items would repeat, and run over and over. How would u do it?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: SELECT REPEAT statement - how do you do it?

Post by Celauran »

Like I said initially, pull down the items you want, stuff them in an array, hand the array off to whatever JS is going to handle your ticker tape. Repeatedly hitting the database to pull down the same set of data doesn't make any sense.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: SELECT REPEAT statement - how do you do it?

Post by simonmlewis »

I'm not using Javascript.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: SELECT REPEAT statement - how do you do it?

Post by Celauran »

simonmlewis wrote:I'm not using Javascript.
a. That doesn't change my answer
2. In your proposed approach, how were you planning on getting subsequent query results without AJAX?
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: SELECT REPEAT statement - how do you do it?

Post by simonmlewis »

Loop the query, around 10-20 times.
So the query finds all the rows, and executes them into a Marquee.
Once it's done that... it then does it again. and again...
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: SELECT REPEAT statement - how do you do it?

Post by simonmlewis »

Something like this:

Code: Select all

echo "<div class='marquee'><marquee behavior='scroll' direction='left' onmouseover=\"this.stop()\" onmouseout=\"this.start()\" scrollamount='3' scrolldelay='20'>";
$marqueecount = 0;
while ($marqueecount < 20)
{
$result = mysql_query ("SELECT * FROM static WHERE section = 'marquee' ORDER BY priority");
  while ($row = mysql_fetch_object($result))
    {
    echo "<a href='$row->url'>$row->freetext</a>&nbsp;|&nbsp;";
    }
$marqueecount ++;
}    
    echo "</marquee></div>";
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: SELECT REPEAT statement - how do you do it?

Post by Celauran »

So you'd be fetching the same data from the database 20 times. That doesn't make any sense. You're fetching data you already have. Take a look at the link I posted to your other thread on this topic, maybe that will help it click.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: SELECT REPEAT statement - how do you do it?

Post by simonmlewis »

Nice design, but it only shows one at a time. We want it to be a continuous movement.
Marquee is a little jerky, I know that.
And I know I am doing the same thing over and over. So how do I put all the rows into an array, and then repeat the array over and over?

Bearing in mind, I need to store the URL and "freetext" as well.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: SELECT REPEAT statement - how do you do it?

Post by simonmlewis »

I think I have cracked it, unless you can see some streamlining?

Code: Select all

$marqueefreetext = array();
$marqueeurl = array();
$result = mysql_query ("SELECT * FROM static WHERE section = 'marquee' ORDER BY priority");
$num_rows = mysql_num_rows($result);
  while ($row = mysql_fetch_object($result))
    {
    $marqueefreetext[] = $row->freetext;
    $marqueeurl[] = $row->url;
    }
    
echo "<div class='marquee'><marquee behavior='scroll' direction='left' onmouseover=\"this.stop()\" onmouseout=\"this.start()\" scrollamount='3' scrolldelay='20'>";
$marqueecount = 0;
while ($marqueecount < $num_rows)
{
echo "<a href='".$marqueeurl[$marqueecount] . "'>" . $marqueefreetext[$marqueecount] . "</a>&nbsp;&nbsp;|&nbsp;&nbsp;";
$marqueecount ++;
}

    echo "</marquee></div>";
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply