...

View Full Version : Is it possible to select an array in ascending order?



elitis
11-10-2010, 12:20 PM
So heres the deal. I have an array with links in it. I want an iframe I have to choose the array values (not the keys) in ascending order. But I only need one, I need the iframe to get the next array value every time I refresh the page. Is this possible? Or is there something else I need to do this?

Array:
<?php
$id=array("http://www.lockerz.com/clickthrough/click/34847/2922","http://www.lockerz.com/clickthrough/click/210197/3098");
?>

Iframe:
<iframe
src="<?php echo $id; ?>">"
width="100%"
height="100%">
</iframe>

mlseim
11-10-2010, 01:31 PM
So for each user, if they refresh their page, they should see the next one?
Does that mean it remembers that when they return back a day later?
Either you'll need to use a cookie to remember their spot, or a PHP SESSION
to remember it, only for that particular visit. Not sure what you are intending to do.

The other part will involve another PHP script.
That script is what will be "sourced" into the <iframe>. And that script will read the
array, and determine which to show based on the cookie or SESSION variable.

When you say "array sort in ascending order", what does that mean?
What part of this is considered to be "ascending"?
.lockerz.com/clickthrough/click/34847/2922



.

elitis
11-10-2010, 10:03 PM
So for each user, if they refresh their page, they should see the next one?
Does that mean it remembers that when they return back a day later?
Either you'll need to use a cookie to remember their spot, or a PHP SESSION
to remember it, only for that particular visit. Not sure what you are intending to do.

The other part will involve another PHP script.
That script is what will be "sourced" into the <iframe>. And that script will read the
array, and determine which to show based on the cookie or SESSION variable.

When you say "array sort in ascending order", what does that mean?
What part of this is considered to be "ascending"?
.lockerz.com/clickthrough/click/34847/2922



.

No. All I want is it to go to the next link in the array every time the page is refreshed. When I said "array sort in ascending order" that is what I meant by it. For it to go to the next link in the array every time the page is refreshed. No saving where they where,etc.

MattF
11-10-2010, 10:09 PM
When I said "array sort in ascending order" that is what I meant by it. For it to go to the next link in the array every time the page is refreshed. No saving where they where,etc.

Regardless of what you thought you meant, that is not sorting in ascending order. You are traversing the array, not sorting it. If you don't save where they were, (which was the last key selected), you can't do it. Each page load has no idea what the last did unless you pass the relevant info to the script each time. That means using the DB, GET, sessions or cookies.

mlseim
11-10-2010, 10:26 PM
Would you like to create a plain text file with a list of your URL's?

So you would have a file called "url.txt" and it would look like this:

http://www.lockerz.com/clickthrough/click/34847/2922
http://www.lockerz.com/clickthrough/click/210197/3098
etc.

You can add to / edit or delete from that text file and the program will
use that file as the "database" for providing the sequential links. You use
Notepad to edit the file or use an online PHP edit script ... not sure what you want.

Let us know if you would like to do it that way.



.

elitis
11-10-2010, 11:27 PM
Regardless of what you thought you meant, that is not sorting in ascending order. You are traversing the array, not sorting it. If you don't save where they were, (which was the last key selected), you can't do it. Each page load has no idea what the last did unless you pass the relevant info to the script each time. That means using the DB, GET, sessions or cookies.
that is why I asked if there was something else I'd need to do.

Would you like to create a plain text file with a list of your URL's?

So you would have a file called "url.txt" and it would look like this:

http://www.lockerz.com/clickthrough/click/34847/2922
http://www.lockerz.com/clickthrough/click/210197/3098
etc.

You can add to / edit or delete from that text file and the program will
use that file as the "database" for providing the sequential links. You use
Notepad to edit the file or use an online PHP edit script ... not sure what you want.

Let us know if you would like to do it that way.
.

Yeah alright. I was basically trying to use the array as my "database" since I couldn't get it to work with my real database

Keleth
11-10-2010, 11:30 PM
Yeah alright. I was basically trying to use the array as my "database" since I couldn't get it to work with my real database

This is a curious point. Why was it not working in your DB?

And a general question: you want the next url based on each users refresh? Or on any refresh? ie, users cycle or the site cycles?

elitis
11-10-2010, 11:47 PM
This is a curious point. Why was it not working in your DB?

And a general question: you want the next url based on each users refresh? Or on any refresh? ie, users cycle or the site cycles?

well technically it was working in my database. but not as I wanted to work. In my other thread (if you remember) I had the links chosen randomly. Once I tried to get the links for this to be chosen from the database in ascending order I assumed every time I refreshed the page (or when anyone refreshed the page) it would go on to the next link like with rand().

But after trying to figure out why it stayed on the same one every time, I figured out that it wouldn't go to the next one because every time I refreshed it would start at the beginning because I limited it to one link. So it did work, but I assumed it would go on to the next link like with rand().

And I want the next url every time the page is refreshed. By javascript refresh, browser refresh, etc.

MattF
11-10-2010, 11:58 PM
Add a timestamp column to the table, update it for the entry each time that specific entry is chosen and sort by the timestamp column.

elitis
11-11-2010, 01:01 AM
Add a timestamp column to the table, update it for the entry each time that specific entry is chosen and sort by the timestamp column.

wait what table? Thought "we" decided to use a plain text file for the urls

Keleth
11-11-2010, 01:14 AM
mlesim suggested a file over an array... donno how any decision being made is anyone but yours. If you are going to use a file but have access to a DB, the DB is most likely the better choice, and Matt merely suggested the method by which you could implement what you tried first with the result you want?

elitis
11-11-2010, 02:40 AM
mlesim suggested a file over an array... donno how any decision being made is anyone but yours. If you are going to use a file but have access to a DB, the DB is most likely the better choice, and Matt merely suggested the method by which you could implement what you tried first with the result you want?

thats why I put quotation marks around 'we'. But if the database is the better choice I guess I'll use that. But I'm a little confused by what matt said. Insert a timestamp column, I get what he means by that. But what does he mean by update it each time the entry is chosen?

mlseim
11-11-2010, 05:08 AM
well technically it was working in my database

So do you have, or do you not have, an existing database?

And is that database MySQL?

==============

The timestamp idea is good for "everyone", but not "individually".
If each user is supposed to have the experience of sequencing,
it has to be a PHP SESSION keeping track of the user's sequence
location in the database.

Let's resolve the "mystery database" problem first.


.

elitis
11-11-2010, 12:01 PM
So do you have, or do you not have, an existing database?

And is that database MySQL?

==============

The timestamp idea is good for "everyone", but not "individually".
If each user is supposed to have the experience of sequencing,
it has to be a PHP SESSION keeping track of the user's sequence
location in the database.

Let's resolve the "mystery database" problem first.


.

I have an existing mysql database. And there really isn't a problem. I assumed that asc would work the same as rand(). Changing every time I refreshed.

MattF
11-11-2010, 01:32 PM
And there really isn't a problem.

There is for the rest of us, namely trying to find out what you actually have and specifically want. You know what you're referring to, we don't, unless you explain precisely and in exact detail.




I assumed that asc would work the same as rand(). Changing every time I refreshed.

It doesn't. It will return results in the same ascending order each time, unless the sort criteria changes. What criteria it sorts on in ascending order is the key, hence using a timestamp column and updating it for each view. Write the code and post it if you encounter problems.

mlseim
11-11-2010, 03:42 PM
Here's an outline ...

First check if a SESSION variable exists for remembering the user's sequence number.
If it exists, determine what it is. Pretend the number is 20.

Query your database for all entries.

Pick the entry number 20 from the query results.
If the entry number does not exists, reset the entry number to 1.
Increment that number to 21 and re-save the SESSION variable.

Display entry number 20 for the user to see.

That's it.

elitis
11-11-2010, 10:02 PM
Here's an outline ...

First check if a SESSION variable exists for remembering the user's sequence number.
If it exists, determine what it is. Pretend the number is 20.

Query your database for all entries.

Pick the entry number 20 from the query results.
If the entry number does not exists, reset the entry number to 1.
Increment that number to 21 and re-save the SESSION variable.

Display entry number 20 for the user to see.

That's it.

when you say user, do u mean a user from a database table called usernames (for example)? If so, there is no logging into an account required to do anything on my site. I'm sorry if I seem like a helpless noob.

mlseim
11-11-2010, 10:21 PM
The user ... I mean site visitor.

No matter who visits the site, they are given a SESSION variable.
A SESSION is like a cookie, but stored on your web server.
It will remember which URL they viewed ... until they close their browser.

elitis
11-12-2010, 12:18 AM
The user ... I mean site visitor.

No matter who visits the site, they are given a SESSION variable.
A SESSION is like a cookie, but stored on your web server.
It will remember which URL they viewed ... until they close their browser.

I tried to keep from having to ask this. But... how do I check for a session variable and where... (and please put it into noob terms so I'll understand better)

mlseim
11-12-2010, 12:59 AM
Give me the name of your MySQL table and the name of the variable (column) you use for the URL's.

I'll make a real code example for you to test.

I have no problem posting code. I just was not sure where you're at with PHP, now I know.
Some people like to learn it by reading books, some like to learn by examining and playing with code.




.

elitis
11-12-2010, 01:47 AM
Give me the name of your MySQL table and the name of the variable (column) you use for the URL's.

I'll make a real code example for you to test.

I have no problem posting code. I just was not sure where you're at with PHP, now I know.
Some people like to learn it by reading books, some like to learn by examining and playing with code.




.

Shop_Decalz (its the table & variable) I like to learn it by playing with code.

mlseim
11-12-2010, 03:22 PM
Here is my untested "shot at it" ...



<?php
session_start();

// Get the next entry for the current user
$seq=0;
if(isset($_SESSION['entry'])){
$seq=$_SESSION['entry'];
}
$_SESSION['entry']=$seq+1;

// Make sure you are connected to your database first.

// Query the database for all URLs
$query="SELECT Shop_Decalz FROM Shop_Decalz";

$result = mysql_query($query) or die(mysql_error());

// Reset sequence if it's more than the number of URLs
if($seq > mysql_num_rows($result)){
$_SESSION['entry']=1;
$seq=0;
}

// Set Pointer To the proper row
if (mysql_data_seek($result, $seq)) {
// Read the row that is being pointed to
$row = mysql_fetch_row($result);
}

echo"This is the URL: {$row['Shop_Decalz']}";


?>

elitis
11-13-2010, 01:18 AM
Here is my untested "shot at it" ...



<?php
session_start();

// Get the next entry for the current user
$seq=0;
if(isset($_SESSION['entry'])){
$seq=$_SESSION['entry'];
}
$_SESSION['entry']=$seq+1;

// Make sure you are connected to your database first.

// Query the database for all URLs
$query="SELECT Shop_Decalz FROM Shop_Decalz";

$result = mysql_query($query) or die(mysql_error());

// Reset sequence if it's more than the number of URLs
if($seq > mysql_num_rows($result)){
$_SESSION['entry']=1;
$seq=0;
}

// Set Pointer To the proper row
if (mysql_data_seek($result, $seq)) {
// Read the row that is being pointed to
$row = mysql_fetch_row($result);
}

echo"This is the URL: {$row['Shop_Decalz']}";


?>


Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/heliox/public_html/projects/database.php:9) in /home/heliox/public_html/projects/test.php on line 6

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/heliox/public_html/projects/database.php:9) in /home/heliox/public_html/projects/test.php on line 6
This is the URL:

mlseim
11-13-2010, 02:10 AM
There must be a duplicate "session_start()" someplace.

Did you include another PHP script?
There can only be one "session_start()" at the very top of the script.

That script I made in post 23 is a test ... put the MySQL connect part in it
and try running it by itself. We have no way of knowing what other scripting you have.

elitis
11-13-2010, 02:36 AM
There must be a duplicate "session_start()" someplace.

Did you include another PHP script?
There can only be one "session_start()" at the very top of the script.

That script I made in post 23 is a test ... put the MySQL connect part in it
and try running it by itself. We have no way of knowing what other scripting you have.
I included a php script that connects to the database.Thats it.

mlseim
11-13-2010, 05:08 AM
There must be a SESSION_START() at the top of that page also.

In your case now, you have to duplicate that other script,
the one you "included". Change the name of it, and remove the session_start()
from that other script. Then, "include" the script you renamed without
the session_start() in it.

You can only have one session_start(), and it needs to be the first thing in the script.
Any other includes cannot contain another session_start().



.

elitis
11-13-2010, 03:51 PM
There must be a SESSION_START() at the top of that page also.

In your case now, you have to duplicate that other script,
the one you "included". Change the name of it, and remove the session_start()
from that other script. Then, "include" the script you renamed without
the session_start() in it.

You can only have one session_start(), and it needs to be the first thing in the script.
Any other includes cannot contain another session_start().



.

this is the script from that page: no session start whatsoever:

<?php
$con = mysql_connect("localhost","blahblahblah","blahblahblah");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("blahblahblah", $con);
?>

mlseim
11-13-2010, 04:54 PM
Show us the whole script that you have now ... the one that gives you the error.

elitis
11-13-2010, 05:29 PM
Show us the whole script that you have now ... the one that gives you the error.



<?php
include("database.php");
?>
<?php
session_start();

// Get the next entry for the current user
$seq=0;
if(isset($_SESSION['entry'])){
$seq=$_SESSION['entry'];
}
$_SESSION['entry']=$seq+1;

// Make sure you are connected to your database first.

// Query the database for all URLs
$query="SELECT Shop_Decalz FROM Shop_Decalz";

$result = mysql_query($query) or die(mysql_error());

// Reset sequence if it's more than the number of URLs
if($seq > mysql_num_rows($result)){
$_SESSION['entry']=1;
$seq=0;
}

// Set Pointer To the proper row
if (mysql_data_seek($result, $seq)) {
// Read the row that is being pointed to
$row = mysql_fetch_row($result);
}

echo"This is the URL: {$row['Shop_Decalz']}";


?>

MattF
11-13-2010, 05:41 PM
You can only have one session_start(), and it ***needs to be the first thing in the script***.


Note what mlseim said earlier.

elitis
11-13-2010, 08:04 PM
Note what mlseim said earlier.

oh right. alright now the code works. But quick question is it suppose to display a url. Or is it suppose to be blank?

mlseim
11-13-2010, 09:15 PM
My example was a simple test.
It should show a different URL each time you refresh the page ... does it?

If it does, you can now use that PHP variable anywhere in your HTML code.
Perhaps it's within an <a href ... > tag? Or, whatever you want.

I don't know what other things your database has, but you can query
anything you wish. Do whatever you want with the data. Hard to say because
we're not sure what your site is about.

elitis
11-14-2010, 02:01 AM
My example was a simple test.
It should show a different URL each time you refresh the page ... does it?

If it does, you can now use that PHP variable anywhere in your HTML code.
Perhaps it's within an <a href ... > tag? Or, whatever you want.

I don't know what other things your database has, but you can query
anything you wish. Do whatever you want with the data. Hard to say because
we're not sure what your site is about.

it does not display any url whatsoever. This is the page of the example. (http://lockerzelites.cz.cc/projects/test.php)

mlseim
11-14-2010, 03:30 AM
Well, now you'll have to do some troubleshooting ....

Put an echo in various places to see what is happening ...

Let's see how many rows it found ...


<?php
session_start();

include("database.php");

// Get the next entry for the current user
$seq=0;
if(isset($_SESSION['entry'])){
$seq=$_SESSION['entry'];
}
$_SESSION['entry']=$seq+1;

// Make sure you are connected to your database first.

// Query the database for all URLs
$query="SELECT Shop_Decalz FROM Shop_Decalz";

$result = mysql_query($query) or die(mysql_error());

// Reset sequence if it's more than the number of URLs
if($seq > mysql_num_rows($result)){
$_SESSION['entry']=1;
$seq=0;
}

// see how many rows it found ...
echo "Rows found: ".mysql_num_rows($result)."<br />";

// Set Pointer To the proper row
if (mysql_data_seek($result, $seq)) {
// Read the row that is being pointed to
$row = mysql_fetch_row($result);
}

echo"This is the URL: {$row['Shop_Decalz']}";


?>


If no rows were found, then the query itself is wrong.

We have no way to test it ourselves, because we don't have your database.
You have to use a little bit of messing around to figure it out.



.

elitis
11-14-2010, 07:31 PM
Well, now you'll have to do some troubleshooting ....

Put an echo in various places to see what is happening ...

Let's see how many rows it found ...


<?php
session_start();

include("database.php");

// Get the next entry for the current user
$seq=0;
if(isset($_SESSION['entry'])){
$seq=$_SESSION['entry'];
}
$_SESSION['entry']=$seq+1;

// Make sure you are connected to your database first.

// Query the database for all URLs
$query="SELECT Shop_Decalz FROM Shop_Decalz";

$result = mysql_query($query) or die(mysql_error());

// Reset sequence if it's more than the number of URLs
if($seq > mysql_num_rows($result)){
$_SESSION['entry']=1;
$seq=0;
}

// see how many rows it found ...
echo "Rows found: ".mysql_num_rows($result)."<br />";

// Set Pointer To the proper row
if (mysql_data_seek($result, $seq)) {
// Read the row that is being pointed to
$row = mysql_fetch_row($result);
}

echo"This is the URL: {$row['Shop_Decalz']}";


?>


If no rows were found, then the query itself is wrong.

We have no way to test it ourselves, because we don't have your database.
You have to use a little bit of messing around to figure it out.



.

it found 23 rows

mlseim
11-14-2010, 09:06 PM
I changed my mind on how to do the script.
Try the one below and refresh your browser.
See if it changes URL's each time.



<?php
session_start();

include("database.php");

// Get the next entry for the current user
$seq=0;
if(isset($_SESSION['entry'])){
$seq=$_SESSION['entry'];
}
$_SESSION['entry']=$seq+1;

// Query the database for all URLs
$query="SELECT Shop_Decalz FROM Shop_Decalz";

$result = mysql_query($query) or die(mysql_error());

// Create a 2-dimensional array
$num_fields = mysql_num_fields($result);
$num_rows = mysql_num_rows($result);
$x=0;
$object=array();
while($row=mysql_fetch_array($result)){
for($j=0;$j<$num_fields;$j++){
$name = mysql_field_name($result, $j);
$object[$x][$name]=stripslashes($row[$name]);
}$x++;
}

// Reset sequence if it's more than the number of URLs
if($seq > $num_rows){
$_SESSION['entry']=1;
$seq=0;
}

echo"This is the URL: {$object[$seq]['Shop_Decalz']}";


?>

elitis
11-15-2010, 02:27 PM
I changed my mind on how to do the script.
Try the one below and refresh your browser.
See if it changes URL's each time.



<?php
session_start();

include("database.php");

// Get the next entry for the current user
$seq=0;
if(isset($_SESSION['entry'])){
$seq=$_SESSION['entry'];
}
$_SESSION['entry']=$seq+1;

// Query the database for all URLs
$query="SELECT Shop_Decalz FROM Shop_Decalz";

$result = mysql_query($query) or die(mysql_error());

// Create a 2-dimensional array
$num_fields = mysql_num_fields($result);
$num_rows = mysql_num_rows($result);
$x=0;
$object=array();
while($row=mysql_fetch_array($result)){
for($j=0;$j<$num_fields;$j++){
$name = mysql_field_name($result, $j);
$object[$x][$name]=stripslashes($row[$name]);
}$x++;
}

// Reset sequence if it's more than the number of URLs
if($seq > $num_rows){
$_SESSION['entry']=1;
$seq=0;
}

echo"This is the URL: {$object[$seq]['Shop_Decalz']}";


?>



haha, it works! :D thanks alot



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum