Jump to content


Photo

PHP Help Please


  • Please log in to reply
29 replies to this topic

#1 rob

rob

    68% Member

  • +Alcohol-Customer
  • 130 posts

Posted 18 April 2006 - 10:57 AM

Hi
I am making a website for my school and our school has blocks for every class (i.e. a block for one student is math and another P.E.) each day our block rotate backwards ( First Day ABCD, Seoncd Day BCDA Third Day CDAB, Fourth Day DABC) I want our website to display the blocks however the blocks only change on weekdays. Is there anyway I can make it display the blocks by rotating every school day? If not how would i make it so I write out every day in advance.)

Thanks for all your Help

Please ask if you don't undestrand

#2 shawn_nee

shawn_nee

    120% Member

  • Alcohol Beta Tester
  • 3,085 posts

Posted 18 April 2006 - 02:21 PM

What about having it rotate for 5 days and for the other 2 doing a full rotation?

Like abcd - bcda - cdab - dabc - abcd
then abcd -abcd and abcd - abcd?

Can you set it to roll for 5 days by 1 then 2 days by 4?

#3 Big Dave

Big Dave

    68% Member

  • +Alcohol-Customer
  • 132 posts

Posted 18 April 2006 - 04:22 PM

Not to sound nasty or anything but, what level are you at with PHP? (ie none, -expert)
It's easier to help someone if you know what they alreay know

#4 rob

rob

    68% Member

  • +Alcohol-Customer
  • 130 posts

Posted 18 April 2006 - 05:55 PM

I have just started php but find it to be fairly similar to c and I am willling to learn as for the above suggestion that would probably work but I don't know how to have it do that

#5 zamiel

zamiel

    Forum Support

  • Administration
  • 8,841 posts

Posted 18 April 2006 - 05:59 PM

Where does this data come from? (ie: database or is it hard coded)

Might help to post any code you actually have.

#6 zamiel

zamiel

    Forum Support

  • Administration
  • 8,841 posts

Posted 18 April 2006 - 06:56 PM

I will assume hard coded. What you should look at is some form of permetation algorithm. However the below will output what shawn_nee is suggesting. Bare in mind this is just one way to do it, I tried to make it as easy to understand as possible:

<?

$rotation = "ABCD";
$end = strlen($start_rotation);

echo $rotation . "<br>";
for ($i=0; $i<$end; $i++) {
$rotation = substr($rotation, 1) . $rotation[0];
echo $rotation . "<br>";
}

?>

If you want to make it change for an extra two days, just change $end to equal 6.

#7 rob

rob

    68% Member

  • +Alcohol-Customer
  • 130 posts

Posted 18 April 2006 - 09:08 PM

QUOTE (zamiel @ Apr 18 2006, 05:53 PM) <{POST_SNAPBACK}>
I will assume hard coded. What you should look at is some form of permetation algorithm. However the below will output what shawn_nee is suggesting. Bare in mind this is just one way to do it, I tried to make it as easy to understand as possible:

<?

$rotation = "ABCD";
$end = strlen($start_rotation);

echo $rotation . "<br>";
for ($i=0; $i<$end; $i++) {
$rotation = substr($rotation, 1) . $rotation[0];
echo $rotation . "<br>";
}

?>

If you want to make it change for an extra two days, just change $end to equal 6.

Alright first of all I don't have any code up second of all i don't know how to pronounce permetation algorithm let alone make one.

I understand some of the stuff up there don't know what
CODE
for ($i=0; $i<$end; $i++) {
  $rotation = substr($rotation, 1) . $rotation[0];
or
CODE
strlen($start_rotation);
does though
thank you for trying to help but a little more explanation would be much appreciated.

Sorry for my Incompedence

#8 zamiel

zamiel

    Forum Support

  • Administration
  • 8,841 posts

Posted 18 April 2006 - 10:00 PM

QUOTE
Alright first of all I don't have any code up second of all i don't know how to pronounce permetation algorithm let alone make one.


Per-me-ta-tion tongue.gif

This simply means given a set of characters or numbers to work out all possible combinations of them.

QUOTE
$end = strlen($start_rotation);


Simply gets the length of the String variable $start_rotation and stores it in $end. This is needed to know when to terminate the loop below. Ofcourse if you want it to continue awhile longer then you simply change this to $end = value. I could've said $end = 4, but if I change $rotation to "ABCDE" I would then have to change $end to 5 to acommodate. This way I don't need to.

QUOTE
for ($i=0; $i<$end; $i++) {
$rotation = substr($rotation, 1) . $rotation[0];
echo $rotation . "<br>";
}


This is a FOR loop. It simply repeats while $i is less than $end. So $i starts at 0, then goes to 1, 2, 3 and exit. $i++ tells i to increment by 1 after each repetition. Iteration is one of the core principles of programming.

Substr simply copies part of a string from a predefined point.

Assume $rotation = "ABCD"

In this case I tell it to start at position 1. The character at position 1 is "B" as strings in PHP start at 0. Not specifying an end in the substr means it starts at position 1 and copies what comes after it. In this case "BCD" so that all that is left to do is to take the character at position 0 and append it to the end of the string. You do this with the "." or concatenation operator. So now we have BCDA. Next time through you will get CDAB and so on.

Does the existing site use PHP?

Can you post a URL of it etc?

#9 rob

rob

    68% Member

  • +Alcohol-Customer
  • 130 posts

Posted 19 April 2006 - 08:44 AM

The origianl site has a little php just for the date and using cute news making it easy for the school to post their own news. http://www.pso.sd27....eetza/index.php
That is my site link big I know. The blocks that are currently up don't rotate. I'll try your suggestions in a little bit when i have more time.

Thanks

#10 rob

rob

    68% Member

  • +Alcohol-Customer
  • 130 posts

Posted 19 April 2006 - 03:59 PM

Alright but all it does is display all the rotations at once how do you get it to display just one per day?

Edited by rob, 19 April 2006 - 04:01 PM.


#11 zamiel

zamiel

    Forum Support

  • Administration
  • 8,841 posts

Posted 19 April 2006 - 08:35 PM

<?

define('mon', 0);
define('tue', 1);
define('wed', 2);
define('thu', 3);
define('fri', 4);
define('sat', 5);
define('sun', 6);

function get_rotation($end) {
$rotation = "ABCD";
for ($i=0; $i<$end; $i++)
$rotation = substr($rotation, 1) . $rotation[0];
return $rotation;
}

echo get_rotation(thu);

?>

You can pass the method above a value (mon-sun) or an integer value and it will return the rotation for that day. You don't have to use the define section, its just so you can use mon-sun instead of 0 - 6.

Alternatively you could use the system date and time to return to get the value you are after, like:

echo get_rotation(date("w", time()));

This uses time() to return a time stamp of the server and "w" to return a integer value that represents the day. 0 = Sun, 1 = Mon. You don't need to use the defines then.

Notice that the define will have Mon = 0, Tue = 1, but the second has the start a day earlier. To fix it, just add 1.

#12 rob

rob

    68% Member

  • +Alcohol-Customer
  • 130 posts

Posted 19 April 2006 - 09:06 PM

Thanks I think that will work have to wait a few days to make shure but I think you got it.
Just one more little bit How do I make it not display the blocks for holiday's like easter and spring break and stuff (I know ill have to code those in seperatly)

#13 zamiel

zamiel

    Forum Support

  • Administration
  • 8,841 posts

Posted 19 April 2006 - 10:23 PM

Some holidays dates never change so before you put out any rotation you simply have to ask if the current date is a holiday. Some holidays like easter change to, so it may be best stored in another file and simply read in so you don't have to modify any actual code.

#14 Jito463

Jito463

    Forum Support

  • Support Team
  • 5,625 posts

Posted 20 April 2006 - 06:34 AM

QUOTE (zamiel @ Apr 18 2006, 10:57 PM) <{POST_SNAPBACK}>
QUOTE
Alright first of all I don't have any code up second of all i don't know how to pronounce permetation algorithm let alone make one.


Per-me-ta-tion tongue.gif

This simply means given a set of characters or numbers to work out all possible combinations of them.


Per-mu-ta-tion laugh.gif tongue.gif

#15 MaLing

MaLing

    Official Birthday Wisher

  • +Alcohol-Customer
  • 2,120 posts

Posted 20 April 2006 - 07:38 AM

Per-me-ta-tion?
Per-me-a-tion?
Per-mu-ta-tion?
tongue.gif

#16 shawn_nee

shawn_nee

    120% Member

  • Alcohol Beta Tester
  • 3,085 posts

Posted 20 April 2006 - 07:45 AM

All depends on your dialect I guess. laugh.gif

Hope all is working on the site. I looked in on it and todays schedule shows DABC, I hope it is correct and is working out for you.

#17 rob

rob

    68% Member

  • +Alcohol-Customer
  • 130 posts

Posted 20 April 2006 - 12:26 PM

I tried
CODE
<?

define('mon', 0);
define('tue', 1);
define('wed', 2);
define('thu', 3);
define('fri', 4);
define('sat', 5);
define('sun', 6);

function get_rotation($end) {
$rotation = "ABCD";
for ($i=0; $i<$end; $i++)
$rotation = substr($rotation, 1) . $rotation[0];
return $rotation;
}

echo get_rotation(thu);

?>


and it didn't change on the next day so im tring the other method right now ill see if it changes tommorow

#18 zamiel

zamiel

    Forum Support

  • Administration
  • 8,841 posts

Posted 20 April 2006 - 03:52 PM

It won't change unless you tell it to, thats what the line below was for:

echo get_rotation(date("w", time()));

It uses the server's time and date to do the change over.

The line echo get_rotation(thu) will not automatically roll over.

So you should have something line:

<?

function get_rotation($end) {
$rotation = "ABCD";
for ($i=0; $i<$end; $i++)
$rotation = substr($rotation, 1) . $rotation[0];
return $rotation;
}

echo get_rotation(date("w", time()));

?>

#19 rob

rob

    68% Member

  • +Alcohol-Customer
  • 130 posts

Posted 20 April 2006 - 05:41 PM

Alright ya thats what im trying now thanks

#20 rob

rob

    68% Member

  • +Alcohol-Customer
  • 130 posts

Posted 21 April 2006 - 11:11 AM

Alright that works great is there anyway for it to repeat the 3rd letter though so instead of abcd to abccd?

Thanks

Edited by rob, 21 April 2006 - 12:11 PM.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users