PDA

View Full Version : Using loops in custom pages.


GriZzm0
11-09-2006, 12:44 PM
I'm using https://vborg.vbsupport.ru/showthread.php?t=62164 to creat my own custom page. I've changed some parts thoo. I use this code instead.
eval('$HTML = "' . fetch_template($templatename) . '";');
eval('print_output("' . fetch_template($shelltemplatename) . '");');
And I've been playing around a littlebit to learn.

if ($_REQUEST['do'] == 'addrequest') {
$templatename = 'job_add_request';

$walle = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "jobs WHERE id = 1");
eval('$test = "'. $walle[test] .'";');
}
Thats what I came up with. It will print out the field "test" from the row with id 1. What if I want to print out all the rows? How do I use a loop in this code? :p
I would like the $test variable in the template to print out the result from "SELECT * FROM " . TABLE_PREFIX . "jobs". And perhaps some html around the result itself. Like. <div>$test</div><div>$test</div> etc.. Anyone know how to do that? :p

CyberRanger
11-09-2006, 01:06 PM
You need to use two templates. The first one formats the individual records. The second one outputs the final result.

$result = $db->query_read("SELECT * FROM " . TABLE_PREFIX . "jobs WHERE 1 = 1 ");
if ($db->num_rows($result) > 0)
{
while ($sub = $db->fetch_array($result))
{
exec_switch_bg();// alternate bgcolor
eval('$wall .= "' . fetch_template('job_add_request_subbit') . '";');
}
eval('$wall .= "' . fetch_template('job_add_request') . '";');
}

The template 'job_add_request_subbit' would have something like:
<div>$sub['test']</div>

while the template 'job_add_request' would be something like:

My Results

{$wall}

GriZzm0
11-09-2006, 01:27 PM
You need to use two templates. The first one formats the individual records. The second one outputs the final result.

$result = $db->query_read("SELECT * FROM " . TABLE_PREFIX . "jobs WHERE 1 = 1 ");
if ($db->num_rows($result) > 0)
{
while ($sub = $db->fetch_array($result))
{
exec_switch_bg();// alternate bgcolor
eval('$wall .= "' . fetch_template('job_add_request_subbit') . '";');
}
eval('$wall .= "' . fetch_template('job_add_request') . '";');
}

The template 'job_add_request_subbit' would have something like:
<div>$result['test']</div>

while the template 'job_add_request' would be something like:

My Results

{$wall}
Thanks. ;) You made one mistake thoo. :p It should be $sub in the templates. Not $result.

CyberRanger
11-09-2006, 01:35 PM
Thanks. ;) You made one mistake thoo. :p It should be $sub in the templates. Not $result.
Oops! Fixed - that's the danger of copy-paste-edit! :-) Oh ... and the "exec_switch_bg();" should be taken out unless you put the results in a table, etc.

GriZzm0
11-09-2006, 08:14 PM
Ok, yet another problem. I would like to print out 6 categories from the databas.
$result = $db->query_read("SELECT * FROM " . TABLE_PREFIX . "jobs WHERE act = 1");
Then I would like to get the content inside the categories.
$resultRequest = $db->query_read("SELECT * FROM " . TABLE_PREFIX . "jobs WHERE act = 0 and category = '". $category['category'] ."' LIMIT 5");

Then I would like to print it out like this.
+-----------------------+-----------------------+-----------------------+
| Category 1 | Category 2 | Category 3 |
+-----------------------+-----------------------+-----------------------+
| Content in | Content in | Content in |
| Category 1 | Category 2 | Category 3 |
+-----------------------+-----------------------+-----------------------+
| Category 4 | Category 5 | Category 6 |
+-----------------------+-----------------------+-----------------------+
| Content in | Content in | Content in |
| Category 4 | Category 5 | Category 6 |
+-----------------------+-----------------------+-----------------------+
Anyone have any idea on how to do that? :p