1

Here is my code. I am trying to connect to my database and make a "joke of the day", so every day it will show a different joke. I've looked through my code several times and can't find out why it's not working.

<div id="joke">
This is the Joke of the Day:
<script>
var jokesArray = new Array ();
<?php
$jokeid = 1;
$link = mysqli_connect("localhost","root","","nick-website-database") or die("Error " . mysqli_error($link));
$temp = mysqli_query($link, "SELECT * FROM `jokes`");
$i = 0;
while($joke = mysqli_fetch_array($temp)) {
    echo "jokesArray[".$i."]='" . $joke["description"]."';" ;
    $i++;
}
?>
var x=new Date();
var d(d.getDay());
if (d === 0) {
    document.write(jokesArray[0];
}
else if (d === 1) {
    document.write(jokesArray[1];
}
    else if (d === 2) {
    document.write(jokesArray[2];
}
    else if (d === 3) {
    document.write(jokesArray[3];
}
    else if (d === 4) {
    document.write(jokesArray[4];
}
    else if (d === 5) {
    document.write(jokesArray[5];
}
    else if (d === 6) {
    document.write(jokesArray[6];
}

</script>
</div>
6
  • 4
    What about it isn't working? Are you getting any errors, have you checked that the script is writing correctly, etc? Commented Aug 14, 2013 at 16:18
  • Also, please edit your title to express your problem and not to add tags. Commented Aug 14, 2013 at 16:19
  • 1
    You should do that on the server, not the client. Commented Aug 14, 2013 at 16:21
  • 1
    Also, you've got tons of syntax errors in your Javascript. Commented Aug 14, 2013 at 16:25
  • it's supposed to print the joke according to the date but it's not printing anything and it doesn't give any errors Commented Aug 14, 2013 at 16:26

4 Answers 4

5

First of all fix this:

var x=new Date();
var d(d.getDay());

to

var x = new Date();
var d  = x.getDay();

Second, everytime you have this document.write(jokesArray[1]; you are not closing it, like this:

document.write(jokesArray[1]);

and instead of doing that HUGE if statement block, just do this, which is the same.

document.write(jokesArray[d]);
Sign up to request clarification or add additional context in comments.

1 Comment

I fixed all the syntax errors but still doesn't print anything
3

All of your document.write's only have an open bracket, you need to close the bracket before the ;

EDIT: Also fix this, you have the date variable wrong

var x=new Date();
var d(d.getDay());

needs to be

var x = new date();
var d = x.getDay();

Javascript is a funny language in the sense that something so simple will break without giving you a warning, try using firefox to debug javascript.

Example:

Your Current Code:

if (d === 0) {
  document.write(jokesArray[0];
}
else if (d === 1) {
  document.write(jokesArray[1];
}
else if (d === 2) {
  document.write(jokesArray[2];
}
else if (d === 3) {
  document.write(jokesArray[3];
}
else if (d === 4) {
  document.write(jokesArray[4];
}
else if (d === 5) {
  document.write(jokesArray[5];
}
else if (d === 6) {
  document.write(jokesArray[6];
}

You need it like this:

The Correct Code:

    if (d === 0) {
  document.write(jokesArray[0]);
}
else if (d === 1) {
  document.write(jokesArray[1]);
}
else if (d === 2) {
  document.write(jokesArray[2]);
}
else if (d === 3) {
  document.write(jokesArray[3]);
}
else if (d === 4) {
  document.write(jokesArray[4]);
}
else if (d === 5) {
  document.write(jokesArray[5]);
}
else if (d === 6) {
  document.write(jokesArray[6]);
}

Hope this helps

1 Comment

I just realized that but it still doesn't work after I closed them all
2

PHP has a getdate() function. See the documentation here. Use conditionals for the date in the PHP code instead of in javascript, then simply "echo jokesarray[x]" where you need it.

Comments

-1

Ok i figured it out. It was a problem with my database. I had apostrophes and dashes in the values so it screwed up the rest of the code. Thanks anyways!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.