944,133 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 1873
  • PHP RSS
Mar 22nd, 2007
0

code output not correct

Expand Post »
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:

PHP Syntax (Toggle Plain Text)
  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:

PHP Syntax (Toggle Plain Text)
  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:

PHP Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
paddyboy is offline Offline
3 posts
since Mar 2007
Mar 23rd, 2007
0

Re: code output not correct

Quote ...
$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.
Reputation Points: 21
Solved Threads: 26
Posting Whiz in Training
Rhyan is offline Offline
240 posts
since Oct 2006
Mar 23rd, 2007
0

Re: code output not correct

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.

Click to Expand / Collapse  Quote originally posted by Rhyan ...
[/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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
paddyboy is offline Offline
3 posts
since Mar 2007
Mar 23rd, 2007
0

Re: code output not correct

Click to Expand / Collapse  Quote originally posted by 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:
PHP Syntax (Toggle Plain Text)
  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 :
PHP Syntax (Toggle Plain Text)
  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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
spincycle is offline Offline
6 posts
since Mar 2007
Mar 23rd, 2007
0

Re: code output not correct

Click to Expand / Collapse  Quote originally posted by spincycle ...

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

PHP Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
paddyboy is offline Offline
3 posts
since Mar 2007
Mar 27th, 2007
0

Re: code output not correct

PHP Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
spincycle is offline Offline
6 posts
since Mar 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: New to php and need some help with a guestbook
Next Thread in PHP Forum Timeline: PHP Help needed badly





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC