HI all, would really appreciate some help.

PLease be gentle, this is my first attempt at programming since the 80's (and that was VB basic! :confused: )

I am using tikiwiki to set up a site. I have modified (butchered?) some existing code to create a new module.

It should return a link to a blog, however it returns "Array".

The module is in 3 parts 1) the function 2) assigning to smarty 3) Smarty. Here is the code:

// Returns the name of "n" random blogs
  function get_random_blogs($n) {
 $query = "select count(*) from `tiki_blogs`";
 $cant = $this->getOne($query,array());
 // Adjust the limit if there are not enough pages
 if ($cant < $n)
     $n = $cant;
 // Now that we know the number of pages to pick select `n`  random positions from `0` to cant
 $positions = array();
 for ($i = 0; $i < $n; $i++) {
     $pick = rand(0, $cant - 1);
     if (!in_array($pick, $positions))
  $positions[] = $pick;
 }
 // Now that we have the positions we just build the data
 $ret = array();
 $temp_max = count($positions);
 for ($i = 0; $i < $temp_max; $i++) {
     $index = $positions[$i];
     $query = "select `blogId` from `tiki_blogs`";
     $name = $this->getOne($query,array(),1,$index);
     $ret[] = $name;
 }
 return $ret;
    }

Then to assign the function to smarty:

<?php
//this script may only be included - so its better to die if called directly.
if (strpos($_SERVER["SCRIPT_NAME"],basename(__FILE__)) !== false) {
  header("location: index.php");
  exit;
}
$n = $tikilib->get_random_blogs($module_rows);
$smarty->assign('modRandomBlogs', $n);
?>

And finally the smarty code:

{* $Header: /cvsroot/tikiwiki/tiki/templates/modules/mod-random_blogs.tpl,v 1.5.10.2 2005/02/23 15:10:56 michael_davey Exp $ *}
{if $feature_wiki eq 'y'}
{tikimodule title="{tr}Correct A Blog{/tr}" name="random_blogs" flip=$module_params.flip decorations=$module_params.decorations}
<table  border="0" cellpadding="0" cellspacing="0">
{section name=ix loop=$modRandomBlogs}
<tr><td   class="module"><a class="linkmodule" href="tiki-view_blog.php?page={$modRandomBlogs [ix].blogId}">{$modRandomBlogs [ix].title}</a></td></tr>
{/section}
</table>
{/tikimodule}
{/if}

Any advice greatly appreciated.

Cheers

Paddyboy

Member Avatar for Rhyan

$positions = array();
for ($i = 0; $i < $n; $i++) {
$pick = rand(0, $cant - 1);
if (!in_array($pick, $positions))
$positions[] = $pick;
}
// Now that we have the positions we just build the data
$ret = array();
$temp_max = count($positions);
for ($i = 0; $i < $temp_max; $i++) {
$index = $positions[$i];
$query = "select `blogId` from `tiki_blogs`";
$name = $this->getOne($query,array(),1,$index);
$ret[] = $name;
}
return $ret;
}

Hi there, I think your problem lies in the manipulation of your arrays.

Look at the code of the positions array you create. Usiing an iteration
is good idea, so why don't you use the $i to set the key of the $positions array.
What I mean is, you should do it like this:

$positions=array()
for ($i = 0; $i < $n; $i++)
  {
  $pick = rand(0, $cant - 1);
  if (!in_array($pick, $positions))
    {
    $positions[$i] = $pick;
    }
  }

The second pard of the script has alsmost the same problem.
Your $ret variable is an array. In order to print out every
elelment of the array you should use some kind of cycle to go
through the array.
In order to display the $ret data, you should do something like:

...
foreach($ret as $result)
  {
  return $result;
  }

Try fixing these, and advise outcome. Note that if $ret is an array,
composed of arrays, you should cycle the arrays contained as well.

Hope it will work.

Cheers Rhyan!

I changed the first part as recommended and no problem.

However the second part.....well you confused me.

when i inserted the code I got the following error;

syntax error, unexpected '}'

I may have inserted it in the wrong place but i tried a number of different variations with similar results.

The second pard of the script has alsmost the same problem. Your $ret variable is an array. In order to print out every elelment of the array you should use some kind of cycle to go through the array. In order to display the $ret data, you should do something like:

foreach($ret as $result)
{
return $result;
}

I just want to pull out 1 element if that makes what i'm trying to do any clearer.

thanks again

paddyboy

Cheers Rhyan!

I changed the first part as recommended and no problem.

However the second part.....well you confused me.

when i inserted the code I got the following error;
syntax error, unexpected '}'
I may have inserted it in the wrong place but i tried a number of different variations with similar results.

I just want to pull out 1 element if that makes what i'm trying to do any clearer.

thanks again

paddyboy

It seems instead of taking $n number of random tiki blog id's you just want one.

This can be done through sql rather simply, depending what implementation you have.

here are two examples you can try:

Syabse:

function get_random_blog() {

$query = "select top 1 'blog_id' from 'tiki_blog' order by newid()";
 
$id = $this->getOne($query,array());
 
return $id;
 
}

Oracle :

function get_random_blog() {

$query = "SELECT 'blog_id' FROM
( SELECT 'blog_id' FROM tiki_blog ORDER BY dbms_random.value )
WHERE rownum = 1";
 
$id = $this->getOne($query,array());
 
return $id;
 
}

you can also check out this website:
http://www.petefreitag.com/item/466.cfm


Hope this helps!


This can be done through sql rather simply, depending what implementation you have.

I have Mysql, so would this work:

function get_random_blogs($n) {
 $query = "select 'blogId' from 'tiki_blogs' order by rand()"
    $id = $this->getOne($query,array());
    return $id;}

I tried the above and got this;
Parse error: syntax error, unexpected T_VARIABLE in ...
cheers

paddyboy

$query = "select 'blogId' from 'tiki_blogs' order by rand()";

you left out a semi-colon at the end of this line.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.