| | |
code output not correct
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Mar 2007
Posts: 3
Reputation:
Solved Threads: 0
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:
Then to assign the function to smarty:
And finally the smarty code:
Any advice greatly appreciated.
Cheers
Paddyboy
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)
// 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 Syntax (Toggle Plain Text)
<?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:
PHP Syntax (Toggle Plain Text)
{* $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
•
•
•
•
$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;
}
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
I miss my mind the most...."
Mark Twain
•
•
Join Date: Mar 2007
Posts: 3
Reputation:
Solved Threads: 0
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
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.
•
•
•
•
[/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]
thanks again
paddyboy
•
•
Join Date: Mar 2007
Posts: 6
Reputation:
Solved Threads: 0
•
•
•
•
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
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)
function get_random_blog() { $query = "select top 1 'blog_id' from 'tiki_blog' order by newid()"; $id = $this->getOne($query,array()); return $id; }
Oracle :
PHP Syntax (Toggle Plain Text)
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!
•
•
Join Date: Mar 2007
Posts: 3
Reputation:
Solved Threads: 0
•
•
•
•
This can be done through sql rather simply, depending what implementation you have.
PHP Syntax (Toggle Plain Text)
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 2:38 pm. Reason: updated
•
•
Join Date: Mar 2007
Posts: 6
Reputation:
Solved Threads: 0
PHP Syntax (Toggle Plain Text)
$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.
![]() |
Similar Threads
- hanoi puzzle help (C++)
- C-Strings : Bug in Code (C++)
- interesting code......Help (C++)
- Plz, helping with writing a bit of code. Thanks (C++)
- Output 2D array? (C++)
- Help with error checking code (C++)
Other Threads in the PHP Forum
- Previous Thread: New to php and need some help with a guestbook
- Next Thread: PHP Help needed badly
| Thread Tools | Search this Thread |
Tag cloud for PHP
.htaccess access ajax apache api array beginner binary broken cakephp checkbox class cms code cron curl database date development directory display download dynamic echo email error file files filter folder form forms function functions gc_maxlifetime google host href htaccess html image include insert integration ip java javascript joomla limit link login loop mail memmory memory menu mlm mod_rewrite multiple mysql navigation oop parse parsing paypal pdf php problem query radio random recursion regex remote script search server sessions sms snippet soap source space sql structure syntax system table thesishelp tutorial update upload url validation validator variable video web xml youtube





