User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 397,818 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,611 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 1289 | Replies: 5
Reply
Join Date: Mar 2007
Location: Uiwang, south korea
Posts: 3
Reputation: paddyboy is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
paddyboy paddyboy is offline Offline
Newbie Poster

Troubleshooting code output not correct

  #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:

 
        // 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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2006
Location: Sofia, Bulgaria
Posts: 135
Reputation: Rhyan is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 7
Rhyan's Avatar
Rhyan Rhyan is offline Offline
Junior Poster

Re: code output not correct

  #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  
Join Date: Mar 2007
Location: Uiwang, south korea
Posts: 3
Reputation: paddyboy is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
paddyboy paddyboy is offline Offline
Newbie Poster

Re: code output not correct

  #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  
Join Date: Mar 2007
Posts: 6
Reputation: spincycle is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
spincycle spincycle is offline Offline
Newbie Poster

Re: code output not correct

  #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:
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!
Reply With Quote  
Join Date: Mar 2007
Location: Uiwang, south korea
Posts: 3
Reputation: paddyboy is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
paddyboy paddyboy is offline Offline
Newbie Poster

Re: code output not correct

  #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:

   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
Last edited by paddyboy : Mar 23rd, 2007 at 1:38 pm. Reason: updated
Reply With Quote  
Join Date: Mar 2007
Posts: 6
Reputation: spincycle is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
spincycle spincycle is offline Offline
Newbie Poster

Re: code output not correct

  #6  
Mar 27th, 2007
 $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 1:34 pm.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb PHP Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 6:40 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC