need help with ssi codeing to Random

Started by johny000, September 07, 2009, 05:13:07 PM

Previous topic - Next topic

SoLoGHoST

Thanks for the detailed explanation of this Arantor.  However, I thought that if you don't select an ORDER BY it won't order it at all.  So you have to specifically state not to order it by using ORDER BY NULL?  And you are saying that this returns rows much faster on MySQL??  Is this only for MySQL and for returned rows that have a column that contains a TEXT type only??  So if I am returning rows that don't contain any TEXT type columns, ordering by NULL wouldn't be appropriate, right??

WoW, Thanks for this, very good to know!

Arantor

It all depends on the situation as to whether it'll be beneficial - your best guide to this is the excellent High Performance MySQL book by Baron Schwartz and others.

If you don't specify an order on a table with TEXT columns, there are instances where it will attempt to sort it by natural order; which will give you a filesort. ORDER BY NULL does it explicitly rather than implicitly; if in doubt, check what it would do using EXPLAIN.

If you had no text fields, in this case it wouldn't make a lot of difference either way. If you do have text fields, ORDER BY NULL when the order doesn't matter. It's pretty selective as to when it would be useful.

johny000

so do you thing you can do it i can wait for it  is ok if is going take some time like a week or moree is ok

or do you know if any one have done it before if you  don't want  do it just give me the links i will  add it  by the way thank you soooooooo much for the  Reply & help you & SoLoGHoST realll goood ppl no jk

Quote from: Arantor on September 08, 2009, 05:45:21 AM
I doubt that's what they did. What I'm suggesting there is the simplest road to implementation. Theirs is probably far more elegant using AJAX to dynamically repopulate the page; doing that is a LOT more work than the three lines my suggestion would take.

SoLoGHoST

#43
johny, if you want to show a post every 3 seconds.  Best not to use either approach.  I suggest querying the database only 1 time, and getting all information you need (what exactly is it that you want to display every 3 seconds??  The actual message??  The subject for the message?? etc...?) and placing it into an array of data, then you can randomly select an index from the array to output every 3 seconds using Javascripts setInterval function.  For example:
echo '<script language="JavaScript" type="text/javascript">
setInterval("doFunc(\'' . addslashes($entry[mt_rand(0, count($entry-1))]) . '\')", 3000);
</script>';


Here's what your new Database query will look like:
include ('SSI.php');

$boards = array(1); // or whatever

$query = db_query("
  SELECT ID_TOPIC, ID_MSG, body
  FROM {$db_prefix}messages
  WHERE ID_BOARD IN (" . implode(', ', $boards) . ")
  ORDER BY NULL
", __FILE__, __LINE__);

$entry = array();

while ($row = mysql_fetch_assoc($query)) {
$row['body'] = parse_bbc($row['body']);
  $entry[] = '<a target="_blank" href="', $scripturl, '?topic=', $row['ID_TOPIC'], '.msg', $row['ID_MSG'], '#msg', $row['ID_MSG'], '" style="text-decoration: none">', $row['body'], '</a>';

}

mysql_free_result($query);


Make sure you have some <div> with an id defined so that the content of that div can be written to every 3 seconds.  For Example:

<div id="message">&nbsp;</div>


Than you can use in the doFunc(message) function

function doFunc(msgBody)
{
document.getElementById("message").innerHTML = msgBody;
}


Should work in all browsers and no need to refresh the page or call the query again and again and again each time.  Just call it once and get all information from it that you will need.

Cheers :)

SoLoGHoST

#44
Here ya go, the entire code for ya.  Please note could take some time to load the page, but after it loads the rest will go smooth...

<?php
include ('SSI.php');

?>

<html>
<head>
<title>TESTING 1 2 3</title>
<?php

$boards 
= array(1); // or whatever

$query db_query("
  SELECT ID_TOPIC, ID_MSG, body 
  FROM 
{$db_prefix}messages
  WHERE ID_BOARD IN (" 
implode(', '$boards) . ")
  ORDER BY NULL
"
__FILE____LINE__);

$entry = array();

while (
$row mysql_fetch_assoc($query)) {
$row['body'] = parse_bbc($row['body']);
  
$entry[] = '<a target="_blank" href="' $scripturl '?topic=' $row['ID_TOPIC'] . '.msg' $row['ID_MSG'] . '#msg' $row['ID_MSG'] . '" style="text-decoration: none">' $row['body'] . '</a>';

}

mysql_free_result($query);

echo 
'<script language="JavaScript" type="text/javascript">
var daEntries = new Array();
'
;

foreach (
$entry as $key => $value)
echo '
daEntries[' 
$key '] = \'' addslashes($value) . '\';';

echo 
'

var maxEntries = \'' 
. (count($entry)-1) . '\';
maxEntries = parseInt(maxEntries);
var minEntries = 0;

function doFunc()
{
var randNum = Math.random()*(maxEntries-minEntries);
randNum = Math.round(minEntries+randNum);

document.getElementById("message").innerHTML = \'\';
document.getElementById("message").innerHTML = daEntries[randNum];
}

function doIt()
{
doFunc();
window.setInterval("doFunc()",3000);
}
</script>'
;

?>

</head>
<body onLoad="doIt();">
<div id="message">&nbsp;</div>
</body>
</html>


There could be a better way of doing this, but for now, this is the only way I could get it to work by copying all of the php $entry array values into the javascript daEntries array.  Also, if you feel it is looping too fast you can increase the time of the setInterval from 3000 to however many milliseconds you want (note each 1000 = 1 second).  I went with Arantor's approach since you seemed to like this 1.  That is the body of the message being a link to the actual message.  However, I still feel that a Subject is needed to complete this.  In anycase if you need a subject, is real easy to add.  You may also want to decorate the <div> tag with CSS and could even place it inside a scrollable area for posts that are really long.  For Example:
<div id="message" style="width: 90%; height: 200px; overflow-y: auto;">  You can decorate it however you like.

Cheers, have tested this and works perfect!  You can see it working here => 1 of my Many Test Sites...

If you are done and pleased with this topic, please mark it Topic Solved.

johny000

hello how are you is me again ihave update to smf v2.0RC2 & now this  code is not working with it  is there is any way you can make it work  & one more thing can you add  same thing like this

$boards = array(160); // Boards to use
$number = 3; // Number of items to return
$length = 75; // Return this number of characters, followed by "...".


& also let say there is photo in the post [img]www.test.net/pic1.jpg[img] will will the photo at will be showing in ' . $row['body'] . '   or just the text
can you make it the photo to thank you sooo much




Quote from: SoLoGHoST on September 10, 2009, 05:44:53 AM
Here ya go, the entire code for ya.  Please note could take some time to load the page, but after it loads the rest will go smooth...

<?php
include ('SSI.php');

?>

<html>
<head>
<title>TESTING 1 2 3</title>
<?php

$boards 
= array(1); // or whatever

$query db_query("
  SELECT ID_TOPIC, ID_MSG, body 
  FROM 
{$db_prefix}messages
  WHERE ID_BOARD IN (" 
implode(', '$boards) . ")
  ORDER BY NULL
"
__FILE____LINE__);

$entry = array();

while (
$row mysql_fetch_assoc($query)) {
$row['body'] = parse_bbc($row['body']);
  
$entry[] = '<a target="_blank" href="' $scripturl '?topic=' $row['ID_TOPIC'] . '.msg' $row['ID_MSG'] . '#msg' $row['ID_MSG'] . '" style="text-decoration: none">' $row['body'] . '</a>';

}

mysql_free_result($query);

echo 
'<script language="JavaScript" type="text/javascript">
var daEntries = new Array();
'
;

foreach (
$entry as $key => $value)
echo '
daEntries[' 
$key '] = \'' addslashes($value) . '\';';

echo 
'

var maxEntries = \'' 
. (count($entry)-1) . '\';
maxEntries = parseInt(maxEntries);
var minEntries = 0;

function doFunc()
{
var randNum = Math.random()*(maxEntries-minEntries);
randNum = Math.round(minEntries+randNum);

document.getElementById("message").innerHTML = \'\';
document.getElementById("message").innerHTML = daEntries[randNum];
}

function doIt()
{
doFunc();
window.setInterval("doFunc()",3000);
}
</script>'
;

?>

</head>
<body onLoad="doIt();">
<div id="message">&nbsp;</div>
</body>
</html>


There could be a better way of doing this, but for now, this is the only way I could get it to work by copying all of the php $entry array values into the javascript daEntries array.  Also, if you feel it is looping too fast you can increase the time of the setInterval from 3000 to however many milliseconds you want (note each 1000 = 1 second).  I went with Arantor's approach since you seemed to like this 1.  That is the body of the message being a link to the actual message.  However, I still feel that a Subject is needed to complete this.  In anycase if you need a subject, is real easy to add.  You may also want to decorate the <div> tag with CSS and could even place it inside a scrollable area for posts that are really long.  For Example:
<div id="message" style="width: 90%; height: 200px; overflow-y: auto;">  You can decorate it however you like.

Cheers, have tested this and works perfect!  You can see it working here => 1 of my Many Test Sites...

If you are done and pleased with this topic, please mark it Topic Solved.

johny000

this what i'm geting Fatal error: Call to undefined function db_query() in 5.php on line 12

the 5.php is the file where i  put the code you give me
<?php
include ('SSI.php');

?>

<html>
<head>
<title>TESTING 1 2 3</title>
<?php

$boards 
= array(1); // or whatever

$query db_query("
SELECT ID_TOPIC, ID_MSG, body 
FROM 
{$db_prefix}messages
WHERE ID_BOARD IN (" 
implode(', '$boards) . ")
ORDER BY NULL
"
__FILE____LINE__);

$entry = array();

while (
$row mysql_fetch_assoc($query)) {
$row['body'] = parse_bbc($row['body']);
$entry[] = '<a target="_blank" href="' $scripturl '?topic=' $row['ID_TOPIC'] . '.msg' $row['ID_MSG'] . '#msg' $row['ID_MSG'] . '" style="text-decoration: none">' $row['body'] . '</a>';

}

mysql_free_result($query);

echo 
'<script language="JavaScript" type="text/javascript">
var daEntries = new Array();
'
;

foreach (
$entry as $key => $value)
echo 
'
daEntries[' 
$key '] = \'' addslashes($value) . '\';';

echo 
'

var maxEntries = \'' 
. (count($entry)-1) . '\';
maxEntries = parseInt(maxEntries);
var minEntries = 0;

function doFunc()
{
var randNum = Math.random()*(maxEntries-minEntries);
randNum = Math.round(minEntries+randNum);

document.getElementById("message").innerHTML = \'\'; 
document.getElementById("message").innerHTML = daEntries[randNum]; 
}

function doIt()
{
doFunc();
window.setInterval("doFunc()",3000);
}
</script>'
;

?>

</head>
<body onLoad="doIt();">
<div id="message">&nbsp;</div>
</body>
</html>

Arantor



Arantor

The code needs a complete rewrite then.

johny000

#50
ok i make it work like this


<?php
include ('SSI.php');
?>

<html>
<head>
<title>TESTING 1 2 3</title>
<?php

// grabbing all topics, posts from board ids 1 and 2!
$boards = array(168);
// get name of boards for output in the bottom area.
$query $smcFunc['db_query']('''
SELECT id_topic, id_msg, body
FROM {db_prefix}messages
WHERE id_board IN ({array_int:id_board})
ORDER BY NULL'
,
array(
'id_board' => $boards,
)
);

$entry = array();

while (
$row mysql_fetch_assoc($query)) {
$row['body'] = parse_bbc($row['body']);
$entry[] = '<a target="_blank" href="' $scripturl '?topic=' $row['id_topic'] . '.msg' $row['id_msg'] . '#msg' $row['id_msg'] . '" style="text-decoration: none">' $row['body'] . '</a>';

}

mysql_free_result($query);

echo 
'<script language="JavaScript" type="text/javascript">
var daEntries = new Array();
'
;

foreach (
$entry as $key => $value)
echo 
'
daEntries[' 
$key '] = \'' addslashes($value) . '\';';

echo 
'

var maxEntries = \'' 
. (count($entry)-1) . '\';
maxEntries = parseInt(maxEntries);
var minEntries = 0;

function doFunc()
{
var randNum = Math.random()*(maxEntries-minEntries);
randNum = Math.round(minEntries+randNum);

document.getElementById("message").innerHTML = \'\'; 
document.getElementById("message").innerHTML = daEntries[randNum]; 
}

function doIt()
{
doFunc();
window.setInterval("doFunc()",3000);
}
</script>'
;

?>

</head>
<body onLoad="doIt();">
<div id="message">&nbsp;</div>
</body>
</html>


one thing ineed to do  iwant to add this how can i do the ?

$number = 3; // Number of items to return
$length = 75; // Return this number of characters, followed by "...".

Quote from: Arantor on February 09, 2010, 03:43:07 AM
The code needs a complete rewrite then.

johny000

Arantor  ihave make it work with smf 2 but can you take look at the  code want to add

$number = 3; // Number of items to return
$length = 75; // Return this number of characters, followed by "...".

can you tell me how  thank you

Arantor

Seeing that this is a 2 line change, I'll do this. Note that I'm generally NOT providing support except where it's fairly easy.

<?php
include ('SSI.php');
?>

<html>
<head>
<title>TESTING 1 2 3</title>
<?php

// grabbing all topics, posts from board ids 1 and 2!
$boards = array(168);
// get name of boards for output in the bottom area.
$query $smcFunc['db_query']('''
SELECT id_topic, id_msg, body
FROM {db_prefix}messages
WHERE id_board IN ({array_int:id_board})
ORDER BY NULL
LIMIT 3'
,
array(
'id_board' => $boards,
)
);

$entry = array();

while (
$row mysql_fetch_assoc($query)) {
$row['body'] = parse_bbc(shorten_subject($row['body'], 75));
$entry[] = '<a target="_blank" href="' $scripturl '?topic=' $row['id_topic'] . '.msg' $row['id_msg'] . '#msg' $row['id_msg'] . '" style="text-decoration: none">' $row['body'] . '</a>';

}

mysql_free_result($query);

echo 
'<script language="JavaScript" type="text/javascript">
var daEntries = new Array();
'
;

foreach (
$entry as $key => $value)
echo 
'
daEntries[' 
$key '] = \'' addslashes($value) . '\';';

echo 
'

var maxEntries = \'' 
. (count($entry)-1) . '\';
maxEntries = parseInt(maxEntries);
var minEntries = 0;

function doFunc()
{
var randNum = Math.random()*(maxEntries-minEntries);
randNum = Math.round(minEntries+randNum);

document.getElementById("message").innerHTML = \'\'; 
document.getElementById("message").innerHTML = daEntries[randNum]; 
}

function doIt()
{
doFunc();
window.setInterval("doFunc()",3000);
}
</script>'
;

?>

</head>
<body onLoad="doIt();">
<div id="message">&nbsp;</div>
</body>
</html>

johny000

this good  but  do you know why is showing from the old post to new how do i make it  read the new post & then the old ones?
Quote from: Arantor on February 10, 2010, 05:51:17 PM
Seeing that this is a 2 line change, I'll do this. Note that I'm generally NOT providing support except where it's fairly easy.

<?php
include ('SSI.php');
?>

<html>
<head>
<title>TESTING 1 2 3</title>
<?php

// grabbing all topics, posts from board ids 1 and 2!
$boards = array(168);
// get name of boards for output in the bottom area.
$query $smcFunc['db_query']('''
SELECT id_topic, id_msg, body
FROM {db_prefix}messages
WHERE id_board IN ({array_int:id_board})
ORDER BY NULL
LIMIT 3'
,
array(
'id_board' => $boards,
)
);

$entry = array();

while (
$row mysql_fetch_assoc($query)) {
$row['body'] = parse_bbc(shorten_subject($row['body'], 75));
$entry[] = '<a target="_blank" href="' $scripturl '?topic=' $row['id_topic'] . '.msg' $row['id_msg'] . '#msg' $row['id_msg'] . '" style="text-decoration: none">' $row['body'] . '</a>';

}

mysql_free_result($query);

echo 
'<script language="JavaScript" type="text/javascript">
var daEntries = new Array();
'
;

foreach (
$entry as $key => $value)
echo 
'
daEntries[' 
$key '] = \'' addslashes($value) . '\';';

echo 
'

var maxEntries = \'' 
. (count($entry)-1) . '\';
maxEntries = parseInt(maxEntries);
var minEntries = 0;

function doFunc()
{
var randNum = Math.random()*(maxEntries-minEntries);
randNum = Math.round(minEntries+randNum);

document.getElementById("message").innerHTML = \'\'; 
document.getElementById("message").innerHTML = daEntries[randNum]; 
}

function doIt()
{
doFunc();
window.setInterval("doFunc()",3000);
}
</script>'
;

?>

</head>
<body onLoad="doIt();">
<div id="message">&nbsp;</div>
</body>
</html>


Arantor

Because you have ORDER BY NULL.

Change that to ORDER BY id_topic DESC instead.

johny000

i change it but is still not working as the way i want it  maybe you did n't get what i'm saying sorry for my bad english

i just add new bored just to show you what i'm talking about.
i have post 4 topic

02-10-2010
02-09-2010
02-08-2010
02-07-2010
with this  code
<?php
include ('SSI.php');
?>

<html>
<head>
<title>TESTING 1 2 3</title>
<?php

// grabbing all topics, posts from board ids 1 and 2!
$boards = array(169);
// get name of boards for output in the bottom area.
$query $smcFunc['db_query']('''
SELECT id_topic, id_msg, body
FROM {db_prefix}messages
WHERE id_board IN ({array_int:id_board})
ORDER BY id_topic
LIMIT 4'
,
array(
'id_board' => $boards,
)
);

$entry = array();

while (
$row mysql_fetch_assoc($query)) {
$row['body'] = parse_bbc(shorten_subject($row['body'], 200));
$entry[] = '<a target="_blank" href="' $scripturl '?topic=' $row['id_topic'] . '.msg' $row['id_msg'] . '#msg' $row['id_msg'] . '" style="text-decoration: none">' $row['body'] . '</a>';

}

mysql_free_result($query);

echo 
'<script language="JavaScript" type="text/javascript">
var daEntries = new Array();
'
;

foreach (
$entry as $key => $value)
echo 
'
daEntries[' 
$key '] = \'' addslashes($value) . '\';';

echo 
'

var maxEntries = \'' 
. (count($entry)-1) . '\';
maxEntries = parseInt(maxEntries);
var minEntries = 0;

function doFunc()
{
var randNum = Math.random()*(maxEntries-minEntries);
randNum = Math.round(minEntries+randNum);

document.getElementById("message").innerHTML = \'\'; 
document.getElementById("message").innerHTML = daEntries[randNum]; 
}

function doIt()
{
doFunc();
window.setInterval("doFunc()",3000);
}
</script>'
;

?>

</head>
<body onLoad="doIt();">
<div id="message">&nbsp;</div>
</body>
</html>



is showing the  post mix not from new(02-10-2010) to old (02-07-2010)

here is the link for the code

http://www.assyrian4all.net/index/6.php

& here is the link for the bored
http://www.assyrian4all.net/index/index.php/board,169.0.html



Quote from: Arantor on February 10, 2010, 07:01:33 PM
Because you have ORDER BY NULL.

Change that to ORDER BY id_topic DESC instead.


Arantor

It is producing a list by topic, in order of the topics being posted. So what exactly do you want?

johny000

the code is not working good   is showing the same post 2 or 3 time & is show the post mix not from new post to the old one is mixing the post up like  the old & then the older & then the new one & the older again

i want it like any ssi at will show the new post & then  the old ones just take look at  the link i give you will get what i'm saying

Quote from: Arantor on February 11, 2010, 02:23:49 AM
It is producing a list by topic, in order of the topics being posted. So what exactly do you want?

Arantor

So do you want it for *topics* or *posts*?

johny000


Advertisement: