/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] On This day in History… Array issues

On This day in History… Array issues

Hello all, can someone spot the mistake in here… cos I can’t!

I am aiming to get an on this day on history function on my site… (eventually to do a google type image change in the header…) but for now theres a bug here and my brain cant see where I am going wrong

Many thanks

Mouse

[code]
<?php

// Array Structure: “Date”,”Quote”,”Year”

$allqts = array
(
“1410”, “tim created script…”, “2006”,
“1510”, “tim posts script on site…”, “2006”,
“1610”, “tim has minimal success…”, “2006”,
);

// Gets the Total number of Items in the array then Divides by 3 because there is a date then a Quote followed by a year
$totalqts = (count($allqts)/3);

// Subtracted 1 from the total because ‘0’ is not accounted for otherwise
$nmbr =($totalqts-1);
$nmbr = $nmbr*3;

// I think this is where it’s going wrong…
$quote = $allqts[$nmbr];
$nmbr = $nmbr+1;
$year = $allqts[$nmbr];
$nmbr = $nmbr+2;

$today = date(dm);

// check var is correct – remove later
echo $today;

if ($today == $allqts) {
echo ‘$quote’, ‘<br /> On this day in $year’ ;
}
else
echo ‘ – oooops no!’;
?>
[/code]

to post a comment
PHP

12 Comments(s)

Copy linkTweet thisAlerts:
@ShmohelOct 14.2006 — [code=php]$allqts = array
(
"1410", "tim created script...", "2006",
"1510", "tim posts script on site...", "2006",
"1610", "tim has minimal success...", "2006",
);[/code]


should be:

[code=php]$allqts = array
(
"1410", "tim created script...", "2006",
"1510", "tim posts script on site...", "2006",
"1610", "tim has minimal success...", "2006"
);[/code]


Note the last item in the array. THere was a comma after it, so it was looking for another array item.
Copy linkTweet thisAlerts:
@Mouse77eauthorOct 14.2006 — 
Note the last item in the array. THere was a comma after it, so it was looking for another array item.[/QUOTE]

You were quite right about the comma but it still wont play....

in place of the Tim created this dumb code

On this day 2006
i still get the default error message.... ooooops

many thanks tho

Mouse
Copy linkTweet thisAlerts:
@ShmohelOct 14.2006 — What is the error message?
Copy linkTweet thisAlerts:
@ShmohelOct 14.2006 — Looks like you're date function is not right.

Can you explain the logic of what you are trying to accomplish?
Copy linkTweet thisAlerts:
@Mouse77eauthorOct 14.2006 — The date function works and the $today value is echoed correctly.

i am trying to get the script to look at the array, see if there is an entry for the date, if so it will echo it into the box on my home page...{ http://www.mouse.nodstrum.com/index.php } if not thae lase else condition will be used to thro in a random quote script to fill the void.

is that a little clearer?

Mouse
Copy linkTweet thisAlerts:
@Mouse77eauthorOct 14.2006 — the i will use a similar set up to change the blue mouse eared image in the banner every once in a while for certain dates... christmas etc.
Copy linkTweet thisAlerts:
@Mouse77eauthorOct 14.2006 — Shmohel... interesting home page.... like the idea! (hic!''')
Copy linkTweet thisAlerts:
@konithomimoOct 14.2006 — I would do it this way:
&lt;?php
// Array Structure: "Date","Quote","Year"
$allqts = array
(
"1410", "tim created script...", "2006",
"1510", "tim posts script on site...", "2006",
"1610", "tim has minimal success...", "2006"
);
$nmbr = sizeof($allqts);
$today = date(dm);

for($i=0;$i&lt;$nmbr;$i+3)
{
$quote = $allqts[$i];
$year = $allqts[$i+2];
if($quote==$today)
{
echo $allqts[$i+1]. '&lt;br/&gt; On this day in '. $year;
return;
}
}
echo ' - oooops no!';
?&gt;
Copy linkTweet thisAlerts:
@NogDogOct 14.2006 — I think you could make life easier by using a mutli-dim array:
[code=php]
$allqts = array
(
'1410' => array
(
'quote' => 'This is a test.',
'year' => '2006'
),
'1510' => array
(
'quote' => 'It is only a test.',
'year' => '2006'
),
'1610' => array
(
'quote' => 'This has been a test.',
'year' => '2006'
)
);

$today = date('dm');
if(array_key_exists($today, $allqts))
{
$quote = $allqts[$today]['quote'];
$year = $allqts[$today]['year'];
}
[/code]
Copy linkTweet thisAlerts:
@ShmohelOct 14.2006 — I was going to suggest doing what NogDog suggests.

Or, even better.... putting it all into a MySQL database and do a query to pull it back. This way, you could build a mini application to add, edit, and delete entries. Might be a lot easier than having to hard code a massive array (as I amssuming there will be more than 3 dates listed).

Glad you like my site. Thanks....
Copy linkTweet thisAlerts:
@NogDogOct 14.2006 — The DB would also make it easy to randomly select an event if you have more than one on the same day.
Copy linkTweet thisAlerts:
@Mouse77eauthorOct 15.2006 — Ladys and gentlemen, once more you have opened my eyes to the glorys in the diversity of PHP... many thanks

Mouse ?
×

Success!

Help @Mouse77e spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.14,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...