Author Topic: append text to URL  (Read 1122 times)

0 Members and 1 Guest are viewing this topic.

NickM

  • Guest
append text to URL
on: Sun, 13. October 2002 - 09:47:35
I import data from a database and would like to carry some of that info to a new page.
In effect I want this
<A HREF = "URL.php?data1=x&data2=y> Now go to the URL carrying data1 and data2 </A>
There must be an easy way - perhaps using <tmpl_varname="varname" escape="URL">
I'm sure this is done all the time, but I'm stumped. Any help would speed up my life and would be appreciated by my family too.
Thanks

releasedj

  • Guest
append text to URL
Reply #1 on: Sun, 13. October 2002 - 10:29:04
In the template do:

Code: [Select]

....

$tmpl-setVar'data1', $data1;

$tmpl-setVar'data2', $data2;



...



.. then in the template do:

Code: [Select]

...

A HREF = URL.php?data1=tmpl_var name=data1 escape=url&data2=tmpl_var name=data2 escape=url Now go to the URL carrying data1 and data2 /A

...



If you're using a WYSIWYG editor, you can use the {TMPL_VAR ..} form of tag.

If this doesn't help, then post your exact scripts and I'll see where it's going wrong.

Regards,

Kelvin

NickM

  • Guest
append text to URL
Reply #2 on: Sun, 13. October 2002 - 13:18:41
Kelvin, fantastic your solution worked.
Now my PHP newbie status is showing :oops:
I keep getting the same link for everything.
Here is the code. I know it is something very simple that I am doing wrong. But I learn quickly!
Here are the main fragments to the code. Somewhere in the looping I am getting confused. I'd appreciate your help.

<?php

// set the name
$tmpl->newLoop('loop1');

// open connection to database
mysql_connect($db_Hostname, $db_UserName, $db_Password) || UhOh("Can't Connect to Database: ".mysql_error());
mysql_select_db($dbase2use);

// get data
$query = "SELECT * FROM $table2use ORDER BY $table2use.Suburb ASC";
$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());

if (mysql_num_rows($result) > 0) {

//start looping through items
while ($row = mysql_fetch_array($result)) {

//add most of the field information for later use

    $id=$row['ID'];
    $location=$row['Suburb'];
    etc.

//assign template names to these fields while looping
$tmpl->setVar('thisID',$id);
$tmpl->setVar('thisSuburb',$location);

// add a row to the loop
$tmpl->addRow($row);

}//end while

$tmpl->addLoop(); // set the loop in the template.
$tmpl->pparse();

}//end if

TEMPLATE
<tmpl_loop name="loop1">

<tr bgcolor="#<tmpl_if name='__EVEN__'>FFFFFF<tmpl_else>FFFF99</tmpl_if>">
<td><a href="rgordon_detailspg.php?ID=<tmpl_var name="thisID" escape="url">&Suburb=<tmpl_var name="thisSuburb" escape="url">">
go there</a></td>
           <td>   <tmpl_var name="Suburb">        </td>
           <td>   <tmpl_var name="Price">         </td>
        </tr>
     </tmpl_loop>

NickM

  • Guest
append text to URL
Reply #3 on: Sun, 13. October 2002 - 15:09:07
:P Discovered the solution! (after a few hours of staring at the monitor)
Place the required URL info into an outer loop.
Now there are two While loops - the first has the URL info and the other is as posted above.

releasedj

  • Guest
append text to URL
Reply #4 on: Mon, 14. October 2002 - 14:12:59
You can do this in a much easier way, your while loop just needs to look like:
Code: [Select]

//start looping through items

while $row = mysql_fetch_array$result



    // add a row to the loop

    $tmpl-addRow$row;



//end while





.. and now in your template, just change the TMPL_VAR name's to exactly what the column is called in the database:

Code: [Select]

a href=rgordon_detailspg.php?ID=tmpl_var name=ID escape=url&Suburb=tmpl_var name=Suburb escape=url

go there/a



You don't need to have a seperate variable, just because you're URL escaping the string... vlibTemplate will handle this on the fly, not changing the variable itself.  :)

Tip:
When you're adding a result directly from a database, you can use the setDbLoop() function (check out the docs). So instead of the while loop in your code, you would put:

Code: [Select]

$tmpl-setDbLoop'loop1', $result, 'MYSQL';



That's a lot simpler.

Regards,

Kelvin

NickM

  • Guest
append text to URL
Reply #5 on: Wed, 16. October 2002 - 12:42:59
Kelvin,
Your suggestion to use setDbLoop works a charm.
Since my previous post I decided (based on your response) to append just the ID in the URL and have the subsequent PHP file use the ID (unique) to obtain all the other information from the table (just one loop).
I thought I'd get smart and repeat your suggestion to use the setDbLoop in the PHP file that received the ID...
In theory the DBase table should open, loop through once and set the variables for that ID. I would then, in the template, change all names to match the fields in the table as you suggested initially, and voila!
Well, it didn't work, and I'm stumped as to why-all seems so logical.
If I use the While-loop option and use the setVar for each of the fields, things work OK - but it's not the most efficient way to do it given your initial suggestion.
Have I got it all wrong?

releasedj

  • Guest
append text to URL
Reply #6 on: Thu, 24. October 2002 - 10:33:37
Remember that all the <TMPL_VAR> name must be identical (and case sensitive) to exactly what you called it in your database.

If in your Database, your ID field is called Id then you must use <tmpl_var name='Id' escape='url'>.

You can check what all your fields are supposed to be called by using :
Code: [Select]

$tmpl = new vlibTemplateDebugtemplate_name;



when you instantiate the class. This will output a console with the entire loop structure where you can see the names.

If you have any more problems then let me know.

Regards,

Kelvin

PS: Sorry for the delayed response, I've just been away for a week.