PDA

View Full Version : Looping templates help


veenuisthebest
08-09-2008, 03:08 AM
I have referred to this (http://www.vbulletin.com/forum/showthread.php?t=235416) and this (https://vborg.vbsupport.ru/showthread.php?t=130012)

Now, in my PHP file, I have this:-

$query=$db->query_read("SELECT * FROM " . TABLE_PREFIX . "mytable ORDER BY `uid` DESC LIMIT " . ($limitlower - 1) . ", $perpage");
while ($result = $db->fetch_array($query))
{
$row_id=$result['uid'];
$row_name=$result['name'];

foreach ($result as $key => $value)
{
eval('$maindata .= "' . fetch_template('loopy_template') . '";');
}}


I have simply copied the foreach part from the above mentioned links.

What arguments I need to pass to foreach and how ? Do I need to do something before it also ? Please guide me on how to correct the above code.

Thank You

RLShare
08-09-2008, 04:09 AM
<a href="http://us3.php.net/foreach" target="_blank">http://us3.php.net/foreach</a>

Opserty
08-09-2008, 07:21 AM
You don't need foreach... unless you are fetching an array from the database.

$query=$db->query_read("SELECT * FROM " . TABLE_PREFIX . "mytable ORDER BY `uid` DESC LIMIT " . ($limitlower - 1) . ", $perpage");
while ($result = $db->fetch_array($query))
{
$row_id = $result['uid'];
$row_name = $result['name'];

eval('$maindata .= "' . fetch_template('loopy_template') . '";');
}
Should work just fine, use $row_id and $row_name in your template. You don't even need those you can just use: $result[uid] or $result[name] directly...

veenuisthebest
08-09-2008, 07:53 AM
yes thanks opserty, I had figured it out earlier with the help of the link in post 2.

can you please tell me:-

Say I have a vbpage called mypage.php , and I have 2 subpages in it called mypage.php?do=this and mypage.php?do=that. So, in total there are 3 associated templates (1 for main page and 2 for subpages).

Now, whenever I visit the subpages I notice that the queries executed in the main page are also being executed in the sub-pages, and the template of the main page is also being used/called unnecessarily in sub-pages (I see this from debug info). BUT its not vice-versa. How should I resolve this ?

Thank you

Guest190829
08-09-2008, 06:44 PM
It is probably the way you have written your conditions, make sure you have a default condition if the request string is empty.


if(empty($_REQUEST['do']))
{
# Go to a default catch all
$_REQUEST['do'] = 'main';
}

if($_REQUEST['do'] == 'main') {
// Main page code
}

if($_REQUEST['do'] == 'sub1') {
// sub page 1 code...

}


// etc...etc...

veenuisthebest
08-10-2008, 02:44 AM
oh wow, thanks danny ! it worked great. Yes, I did not had a default condition if the request string was empty.

alright one more thing plz...

How do I append a variable to a URL from within PHP?
see I have this code:-

if ($_REQUEST['do'] == 'search')
{
$category=$_REQUEST["cat"];
$order=$_REQUEST["order"];
}

$category and $order contains the values posted by the form via post method. I want that these values should append to URL like this "page.php?do=search&amp;cat=$category&amp;order=$order".

Currently it does not show the values in the URL (its blank like cat=), but on echo the variable does throw the correct values.

Thank You

RLShare
08-10-2008, 02:49 AM
First try concatenating the string...ex... "?do=search&cat=".$category."&"
Also use the URLencode function before you combine the string to form the url
http://us3.php.net/urlencode

veenuisthebest
08-10-2008, 03:46 AM
where do I put this line "?do=search&cat=".$category."&" in the php code above ?? How to append to URL ?

Dismounted
08-10-2008, 06:24 AM
On the code for the link to the page - it needs to have the variables in the URL there.

veenuisthebest
08-11-2008, 01:13 AM
sorry I don't understand stilll ..

see I have a form (in template) whose action is "page.php?do=search" via post method. On hitting Submit button it simply goes to the below code, executes the query and all, and results fine.

if ($_REQUEST['do'] == 'search')
{
$category=$_REQUEST["cat"];
$order=$_REQUEST["order"];
}


Now, my problem is that the URL of the results page displays page.php?do=search and NOT appending the variables, how do I do it ??? how do i append $category and $order values to it ??

Thank You

RLShare
08-11-2008, 01:35 AM
oh ok lol... Change the forms method from 'POST' to 'GET'. Aslo change the action from 'page.php?do=search' to just 'page.php' and add a hidden input with the name 'do' that has the value set as 'search'.

veenuisthebest
08-11-2008, 02:26 AM
ahh.. thank you so much !! I had used get method earlier but did not change action or added that hidden field. It does work now. You solved a big problem of mine.

alright.. one more thing, GET method adds all the fields in the URL. How do I remove some ? for ex. that long securitytoken value also appends now. I must use that but do not want to show that in URL.

Thank You

RLShare
08-11-2008, 03:08 AM
You do not need the security tokens in a 'get' form.

veenuisthebest
08-11-2008, 12:46 PM
oh great.. everything works now !!

few more things plzz :-

1. How do I make URL parameters mandatory or say redirect to main page or give an error message. Say, I have a page with a form that works only like page.php?do=edit&amp;uid=50. Now, I do not want it to be opened/viewed like page.php?do=edit as it shows a blank form.

2. I wanted to know, when is $db->close(); used ?

RLShare
08-11-2008, 07:20 PM
Judging by your url, I have one comment about it. The url makes it seem as if your editing something on the page, I would go back to using the POST request or implement your own CRSF protection on the GET request if your page allows for editing of anything important. VB's CSRF does not cover get requests only post requests.


1.
if(empty($_REQUEST['uid'])){
//there was no uid sent to the form

}

2. when your finished with the database.

veenuisthebest
08-12-2008, 01:16 AM
yes I am already using POST method there for the above "edit form page" and have implemented the CSRF protection.

1. Great.. DONE !

2. In my page, its like this:-

if(empty($_REQUEST['do']))
{
# Go to a default catch all
$_REQUEST['do'] = 'main';
}

if($_REQUEST['do'] == 'main') {
// Main page code
}

if($_REQUEST['do'] == 'sub1') {
// sub page 1 code...

}

// and so on.....

So, where do I put $db->close(); ? at the end of every subpage ? or just once at the very end above ?>

Thank You

RLShare
08-12-2008, 02:00 AM
From what I understand you do not need to use that unless your accessing the database in your code. And all that function does is clean up and close the connection to the database.

If Im wrong someone may correct me.

veenuisthebest
08-12-2008, 03:08 AM
but I am accessing database in my code, lot of queries are there !

Also, please tell, is there a way to insert escape charaters in the database by NOT using $vbulletin->db->escape_string_like(htmlspecialchars_uni($variable) ) in the query, as it makes it very long when there are many fields.

Thanks