Jump to content

Recommended Posts

Hello, I am trying to make a Did You Know system that uses MySql to load the stuff, and Javascript to randomize it.

 

Im trying to set it up so the script counts the total amount of items in the database, then "echo's" it into the javascript code. Then it also "Echo's" the Id, and the Description of it.

 

My Code

<?php
DEFINE ('DB_HOST', 'localhost'); // This will most likely stay the same.
DEFINE ('DB_USER', 'root'); // Insert your database username into the quotes.
DEFINE ('DB_PASSWORD', ''); // Insert your database password into the quotes.
DEFINE ('DB_NAME', 'test');// Insert your actual database name in the quotes.
$dbc = mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) OR die ('Could not connect to MySQL: ' . mysql_error());
mysql_select_db (DB_NAME) OR die('Could not select the database: ' . mysql_error() );
?>

<?php

$list_q = mysql_query("SELECT * FROM dyk") or die (mysql_error());

while($list_f = mysql_fetch_assoc($list_q)) {

		list($total_count) = mysql_fetch_row(mysql_query("SELECT COUNT(id) FROM dyk"));

	}

?>


<script language="javascript"> 
rnd.today=new Date();
rnd.seed=rnd.today.getTime();
function rnd() {
        rnd.seed = (rnd.seed*9301+49297) % 233280;
        return rnd.seed/(233280.0);
};

function rand(number) {
   var result = Math.ceil(rnd()*number);
   if (!result)result++;
        return result
};
var ad_cnt1 = <?php echo '. $total_count .'?>;
var ad1 = rand(ad_cnt1);
var dyk1;
var link1;
var desc1;

if (ad1==<?php echo "'$list_f['id']'"?>) {
dyk1="<?php echo "'$list_f['dyk']'"?>";
}

document.write('' + dyk1 + '');
</script>

 

Error

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\xampp\htdocs\dyk.php on line 42

Link to comment
https://forums.phpfreaks.com/topic/205950-did-you-know/
Share on other sites

Fixed the code, but now nothing is showing up, It's just a blank page. Here's my code now.

 

<?php
DEFINE ('DB_HOST', 'localhost'); // This will most likely stay the same.
DEFINE ('DB_USER', 'root'); // Insert your database username into the quotes.
DEFINE ('DB_PASSWORD', ''); // Insert your database password into the quotes.
DEFINE ('DB_NAME', 'test');// Insert your actual database name in the quotes.
$dbc = mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) OR die ('Could not connect to MySQL: ' . mysql_error());
mysql_select_db (DB_NAME) OR die('Could not select the database: ' . mysql_error() );
?>

<?php

$list_q = mysql_query("SELECT * FROM dyk") or die (mysql_error());

while($list_f = mysql_fetch_assoc($list_q)) {

		list($total_count) = mysql_fetch_row(mysql_query("SELECT COUNT(id) FROM dyk"));

	}

?>


<script language="javascript"> 
rnd.today=new Date();
rnd.seed=rnd.today.getTime();
function rnd() {
        rnd.seed = (rnd.seed*9301+49297) % 233280;
        return rnd.seed/(233280.0);
};

function rand(number) {
   var result = Math.ceil(rnd()*number);
   if (!result)result++;
        return result
};
var ad_cnt1 = <?php echo  $total_count; ?>;
var ad1 = rand(ad_cnt1);
var dyk1;

if (ad1==<?php echo $list_f['id'];?>) {
dyk1="<?php echo $list_f['dyk'];?>";
}

document.write('' + dyk1 + '');
</script>

Link to comment
https://forums.phpfreaks.com/topic/205950-did-you-know/#findComment-1077685
Share on other sites

You can't embed array references like this:

if (ad1==<?php echo "'$list_f['id']'"?>) {
dyk1="<?php echo "'$list_f['dyk']'"?>";
}

Use either concatenation:

if (ad1==<?php echo "'" . $list_f['id'] . "'"?>) {
dyk1="<?php echo "'" . $list_f['dyk'] . "'"?>";
}

or curly brackets "{ }"

if (ad1==<?php echo "'{$list_f['id']}'"?>) {
dyk1="<?php echo "'{$list_f['dyk']}'"?>";
}

 

Ken

Link to comment
https://forums.phpfreaks.com/topic/205950-did-you-know/#findComment-1077686
Share on other sites

Look at this section:

var ad_cnt1 = <?php echo $total_count; ?>;
var ad1 = rand(ad_cnt1);
var dyk1;
var link1;
var desc1;

if(ad1 == "<?php echo $list_f['id']; ?>");
dyk1="<?php echo $list_f['dyk']; ?>";

 

You could also do it this way.

 

$list_q = mysql_query("SELECT * FROM dyk") or die (mysql_error());
while($list_f = mysql_fetch_assoc($list_q)) {	
$storeArray[] = $list_f['dyk'];
}
$random_number = rand(0,(count($storeArray) - 1));
echo $storeArray[$random_number];

Link to comment
https://forums.phpfreaks.com/topic/205950-did-you-know/#findComment-1077687
Share on other sites

You can't embed array references like this:

if (ad1==<?php echo "'$list_f['id']'"?>) {
dyk1="<?php echo "'$list_f['dyk']'"?>";
}

Use either concatenation:

if (ad1==<?php echo "'" . $list_f['id'] . "'"?>) {
dyk1="<?php echo "'" . $list_f['dyk'] . "'"?>";
}

or curly brackets "{ }"

if (ad1==<?php echo "'{$list_f['id']}'"?>) {
dyk1="<?php echo "'{$list_f['dyk']}'"?>";
}

 

Ken

 

It's still not wanting to show what I have in the database, I have two of them in there, (ID = 1 and 2) and (dyk = Test 1 and Test 2) so it should randomy choose one amd Echo it, but's it's not, it's just showing a blank page.

Link to comment
https://forums.phpfreaks.com/topic/205950-did-you-know/#findComment-1077689
Share on other sites

Look at this section:

var ad_cnt1 = <?php echo $total_count; ?>;
var ad1 = rand(ad_cnt1);
var dyk1;
var link1;
var desc1;

if(ad1 == "<?php echo $list_f['id']; ?>");
dyk1="<?php echo $list_f['dyk']; ?>";

 

You could also do it this way.

 

$list_q = mysql_query("SELECT * FROM dyk") or die (mysql_error());
while($list_f = mysql_fetch_assoc($list_q)) {	
$storeArray[] = $list_f['dyk'];
}
$random_number = rand(0,(count($storeArray) - 1));
echo $storeArray[$random_number];

Thanks to both of you. :)

Link to comment
https://forums.phpfreaks.com/topic/205950-did-you-know/#findComment-1077690
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.