code output not correct

Reply

Join Date: Mar 2007
Posts: 3
Reputation: paddyboy is an unknown quantity at this point 
Solved Threads: 0
paddyboy paddyboy is offline Offline
Newbie Poster

code output not correct

 
0
  #1
Mar 22nd, 2007
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! )

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:

  1.  
  2. // Returns the name of "n" random blogs
  3. function get_random_blogs($n) {
  4. $query = "select count(*) from `tiki_blogs`";
  5. $cant = $this->getOne($query,array());
  6. // Adjust the limit if there are not enough pages
  7. if ($cant < $n)
  8. $n = $cant;
  9. // Now that we know the number of pages to pick select `n` random positions from `0` to cant
  10. $positions = array();
  11. for ($i = 0; $i < $n; $i++) {
  12. $pick = rand(0, $cant - 1);
  13. if (!in_array($pick, $positions))
  14. $positions[] = $pick;
  15. }
  16. // Now that we have the positions we just build the data
  17. $ret = array();
  18. $temp_max = count($positions);
  19. for ($i = 0; $i < $temp_max; $i++) {
  20. $index = $positions[$i];
  21. $query = "select `blogId` from `tiki_blogs`";
  22. $name = $this->getOne($query,array(),1,$index);
  23. $ret[] = $name;
  24. }
  25. return $ret;
  26. }

Then to assign the function to smarty:

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

And finally the smarty code:

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

Any advice greatly appreciated.

Cheers

Paddyboy
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 232
Reputation: Rhyan is an unknown quantity at this point 
Solved Threads: 24
Rhyan's Avatar
Rhyan Rhyan is offline Offline
Posting Whiz in Training

Re: code output not correct

 
0
  #2
Mar 23rd, 2007
$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:
[php]
$positions=array()
for ($i = 0; $i < $n; $i++)
{
$pick = rand(0, $cant - 1);
if (!in_array($pick, $positions))
{
$positions[$i] = $pick;
}
}
[/php] 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:

[php]
...
foreach($ret as $result)
{
return $result;
}
[/php]
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.
" Of all the things I've lost,
I miss my mind the most...."
Mark Twain
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 3
Reputation: paddyboy is an unknown quantity at this point 
Solved Threads: 0
paddyboy paddyboy is offline Offline
Newbie Poster

Re: code output not correct

 
0
  #3
Mar 23rd, 2007
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.

Originally Posted by Rhyan View Post
[/php] 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:

[php]
...
foreach($ret as $result)
{
return $result;
}
[/php]
I just want to pull out 1 element if that makes what i'm trying to do any clearer.

thanks again

paddyboy
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 6
Reputation: spincycle is an unknown quantity at this point 
Solved Threads: 0
spincycle spincycle is offline Offline
Newbie Poster

Re: code output not correct

 
0
  #4
Mar 23rd, 2007
Originally Posted by paddyboy View Post
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:
  1. function get_random_blog() {
  2.  
  3. $query = "select top 1 'blog_id' from 'tiki_blog' order by newid()";
  4.  
  5. $id = $this->getOne($query,array());
  6.  
  7. return $id;
  8.  
  9. }


Oracle :
  1.  
  2. function get_random_blog() {
  3.  
  4. $query = "SELECT 'blog_id' FROM
  5. ( SELECT 'blog_id' FROM tiki_blog ORDER BY dbms_random.value )
  6. WHERE rownum = 1";
  7.  
  8. $id = $this->getOne($query,array());
  9.  
  10. return $id;
  11.  
  12. }



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


Hope this helps!
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 3
Reputation: paddyboy is an unknown quantity at this point 
Solved Threads: 0
paddyboy paddyboy is offline Offline
Newbie Poster

Re: code output not correct

 
0
  #5
Mar 23rd, 2007
Originally Posted by spincycle View Post

This can be done through sql rather simply, depending what implementation you have.
I have Mysql, so would this work:

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

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

paddyboy
Last edited by paddyboy; Mar 23rd, 2007 at 2:38 pm. Reason: updated
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 6
Reputation: spincycle is an unknown quantity at this point 
Solved Threads: 0
spincycle spincycle is offline Offline
Newbie Poster

Re: code output not correct

 
0
  #6
Mar 27th, 2007
  1. $query = "select 'blogId' from 'tiki_blogs' order by rand()";


you left out a semi-colon at the end of this line.
Last edited by spincycle; Mar 27th, 2007 at 2:34 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC