News:

Wondering if this will always be free?  See why free is better.

Main Menu

need help with ssi codeing to Random

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

Previous topic - Next topic

johny000


Arantor

Which code do you want to use, mine or SoLoGHoST's?

For mine, take the last entry I posted, remove the comma after ORDER BY NULL

For SoLoGHoST's, take the code you already have, and replace ID_BORAD with ID_BOARD.

johny000

i have  replace ID_BORAD with ID_BOARD.

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

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

$query db_query("
  SELECT COUNT(*) FROM 
{$db_prefix}messages WHERE ID_BOARD IN (" implode(', '$boards) . ")
"
__FILE____LINE__);

$entry 0;
if(
$row mysql_fetch_row($query)) {
  
$entry mt_rand(0$row[0]-1);
}
mysql_free_result($query);

$query db_query("
  SELECT * FROM 
{$db_prefix}messages
  WHERE ID_BOARD. IN (" 
implode(', '$boards) . ")"__FILE____LINE__);

$x 0;

while (
$row mysql_fetch_assoc($request)) {
if (
$entry == $x)
{
// do the stuff...
}

$x++;
}
?>




how is telling me
You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'IN (1)' at line 2
File: htdocs/smf/fnews1.php
Line: 18

Quote from: Arantor on September 07, 2009, 09:56:14 PM
Which code do you want to use, mine or SoLoGHoST's?

For mine, take the last entry I posted, remove the comma after ORDER BY NULL

For SoLoGHoST's, take the code you already have, and replace ID_BORAD with ID_BOARD.

Arantor

Take the full stop off - I was just ending the sentence.

WHERE ID_BOARD IN

johny000

idid not get it can you give me the full code
Quote from: Arantor on September 07, 2009, 10:16:33 PM
Take the full stop off - I was just ending the sentence.

WHERE ID_BOARD IN

Arantor

I already did give you the full code - several posts back, including how to fix the one error in it. Do you want to use my code or SoLoGHoST's?

johny000

your code is still giveing me the Error can you fix it 100% & give ti again
Quote from: Arantor on September 07, 2009, 10:21:46 PM
I already did give you the full code - several posts back, including how to fix the one error in it. Do you want to use my code or SoLoGHoST's?

Arantor


johny000

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

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

$query db_query("
  SELECT COUNT(*) FROM 
{$db_prefix}messages WHERE ID_BOARD IN (" implode(', '$boards) . ")
"
__FILE____LINE__);

$entry 0;
if(
$row mysql_fetch_row($query)) {
  
$entry mt_rand(0$row[0]-1);
}
mysql_free_result($query);

$query db_query("
  SELECT * FROM 
{$db_prefix}messages
  WHERE ID_BOARD. IN (" 
implode(', '$boards) . ")"__FILE____LINE__);

$x 0;

while (
$row mysql_fetch_assoc($request)) {
if (
$entry == $x)
{
// do the stuff...
}

$x++;
}
?>




Quote from: Arantor on September 07, 2009, 10:31:24 PM
Which code are you using now?

Arantor

That's not my code at all. Thus, it's incomplete.


Fixed version of my code:
<?php
include ('SSI.php');

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

$query db_query("
  SELECT COUNT(*) FROM 
{$db_prefix}messages WHERE ID_BOARD IN (" implode(', '$boards) . ")
"
__FILE____LINE__);

$entry 0;
if(
$row mysql_fetch_row($query)) {
  
$entry mt_rand(0$row[0]-1);
}
mysql_free_result($query);

$query db_query("
  SELECT * FROM 
{$db_prefix}messages
  WHERE ID_BORAD IN (" 
implode(', '$boards) . ")
  ORDER BY NULL
  LIMIT 
{$entry},1
"
__FILE____LINE__);

if(
$row mysql_fetch_assoc($query)) {
  
// $row = nice friendly array of content about the message, including its title, board id, and much more,
  // see the messages table for a complete list of what you get
}
mysql_free_result($query);
?>


You'll have to decide what you want to do with the contents of $row at the end though and how you display it. I can't write that for you because I don't know how you want to display the post.

johny000

i want display  like this

<a target="_blank" href="', $news['href'], '" style="text-decoration: none">', $news['body'], '</a>

so where will iadd the code?

Quote from: Arantor on September 07, 2009, 10:39:59 PM
That's not my code at all. Thus, it's incomplete.


Fixed version of my code:
<?php
include ('SSI.php');

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

$query db_query("
  SELECT COUNT(*) FROM 
{$db_prefix}messages WHERE ID_BOARD IN (" implode(', '$boards) . ")
"
__FILE____LINE__);

$entry 0;
if(
$row mysql_fetch_row($query)) {
  
$entry mt_rand(0$row[0]-1);
}
mysql_free_result($query);

$query db_query("
  SELECT * FROM 
{$db_prefix}messages
  WHERE ID_BORAD IN (" 
implode(', '$boards) . ")
  ORDER BY NULL
  LIMIT 
{$entry},1
"
__FILE____LINE__);

if(
$row mysql_fetch_assoc($query)) {
  
// $row = nice friendly array of content about the message, including its title, board id, and much more,
  // see the messages table for a complete list of what you get
}
mysql_free_result($query);
?>


You'll have to decide what you want to do with the contents of $row at the end though and how you display it. I can't write that for you because I don't know how you want to display the post.

Arantor

You can't do it as simply as that, not at all. This hasn't come through the nice tidy SSI method, so the 'href' and 'body' don't exist properly.

However:


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

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

$query db_query("
  SELECT COUNT(*) FROM 
{$db_prefix}messages WHERE ID_BOARD IN (" implode(', '$boards) . ")
"
__FILE____LINE__);

$entry 0;
if(
$row mysql_fetch_row($query)) {
  
$entry mt_rand(0$row[0]-1);
}
mysql_free_result($query);

$query db_query("
  SELECT * FROM 
{$db_prefix}messages
  WHERE ID_BORAD IN (" 
implode(', '$boards) . ")
  ORDER BY NULL
  LIMIT 
{$entry},1
"
__FILE____LINE__);

if(
$row mysql_fetch_assoc($query)) {
  
$row['body'] = parse_bbc($row['body']);
  echo 
'<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);
?>


This will turn the entire contents of the post into a huge link back to the thread. I don't think that's quite what you're looking for but it's what you've requested.

johny000

i'm geting this  Error
Unknown column 'ID_BORAD' in 'where clause'
File: htdocs/smf/fnews1.php
Line: 21

ok let me make it more clear all i want   is when i post   new post in  let say bored ID 150

it will show like this website  http://www.ishtartv.com/ as Random here the pic i take so you know watch one


Arantor

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

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

$query db_query("
  SELECT COUNT(*) FROM 
{$db_prefix}messages WHERE ID_BOARD IN (" implode(', '$boards) . ")
"
__FILE____LINE__);

$entry 0;
if(
$row mysql_fetch_row($query)) {
  
$entry mt_rand(0$row[0]-1);
}
mysql_free_result($query);

$query db_query("
  SELECT * FROM 
{$db_prefix}messages
  WHERE ID_BOARD IN (" 
implode(', '$boards) . ")
  ORDER BY NULL
  LIMIT 
{$entry},1
"
__FILE____LINE__);

if(
$row mysql_fetch_assoc($query)) {
  
$row['body'] = parse_bbc($row['body']);
  echo 
'<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);
?>


*slaps face* I made the same typo, ID_BORAD not ID_BOARD.

So does every post in that board contain a picture like that?

johny000

ok now this  code  is working how do i make it switch  let say every  3 secondt to the  another post

Quote from: Arantor on September 07, 2009, 11:19:06 PM
<?php
include ('SSI.php');

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

$query db_query("
  SELECT COUNT(*) FROM 
{$db_prefix}messages WHERE ID_BOARD IN (" implode(', '$boards) . ")
"
__FILE____LINE__);

$entry 0;
if(
$row mysql_fetch_row($query)) {
  
$entry mt_rand(0$row[0]-1);
}
mysql_free_result($query);

$query db_query("
  SELECT * FROM 
{$db_prefix}messages
  WHERE ID_BOARD IN (" 
implode(', '$boards) . ")
  ORDER BY NULL
  LIMIT 
{$entry},1
"
__FILE____LINE__);

if(
$row mysql_fetch_assoc($query)) {
  
$row['body'] = parse_bbc($row['body']);
  echo 
'<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);
?>


*slaps face* I made the same typo, ID_BORAD not ID_BOARD.

So does every post in that board contain a picture like that?

Arantor

By putting that in an iframe and adding some Javascript to refresh it (and hope your server doesn't get flooded with queries)

johny000

is thit what they did?


Quote from: Arantor on September 07, 2009, 11:37:43 PM
By putting that in an iframe and adding some Javascript to refresh it (and hope your server doesn't get flooded with queries)

Arantor

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

Just now getting back to this topic...hehe.  Yeah, Johnny, Arantor's code is much more efficient than mine, definitely use his.  Sorry for the confusion.

Didn't know about the ORDER BY NULL.  Are you are saying it is best to use an ORDER BY when querying a table and if there is nothing to orderby just do an ORDER BY NULL??  Otherwise, could cause a filesort problem??  Just wondering...

And Thanks :)

Arantor

SoLoGHoST: It really, really depends on what you're doing, and what I'm about to say is MySQL specific.

If you want to order records, you have to sort them, and if you're pulling any record that contains a TEXT (TINYTEXT, TEXT, MEDIUMTEXT, LARGETEXT) column, MySQL will sort it on the disk in a temporary table before serving to the application. As you can imagine this is hideous for I/O performance because you're spending time pushing it on disk - even if you're ordering by primary key of the table.

In some cases you can't avoid it much; e.g. thread display you can't avoid it. For other displays (message index) one of the tips given in the awesome thread by Vekseid is that you convert TINYTEXT to VARCHAR(255) which basically sidesteps this issue for the most part, because MySQL can sort that in memory.

In this case we're stepping through the table row by row, no matter what we do, this is what's known as a table scan. Now in this specific case we don't care what order they're in, simply that we return an arbitrary row. Since we're already scanning the table, we want to specifically tell MySQL not to bother ordering the table, we'll just have it in any old order - because it doesn't matter. This is important because the messages table contains a TEXT column, and sorting on that, even on primary key or other defined unique index is going to start being painful once you get beyond a few hundred rows.

Advertisement: