PDA

View Full Version : Help passing variables between templates..


DustyJoe
01-01-2012, 03:07 AM
I've been searching for a bit but have not come across anything that really matches my dilemma.

My code probably isn't the greatest as I am learning as I go, please be kind..

Here is where my issue begins, I can read data from my database and pass it to the template to be displayed within a table
$query = "SELECT * FROM BAMFG_vehicle";
$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result)){
$output .= "<tr><td width=100>".$row['year']."</td><td width=100>".$row['make_model']."</td><td width=20>".$row['vehicle_id']."</td><td width=100><a href='bamfg_vehicle.php?do=delete_vehicle&id=".$row['vehicle_id']."'>DELETE</a></td><td width=100><a href='bamfg_vehicle.php?do=edit_vehicle&id=".$row['vehicle_id']."'>EDIT</a></td></tr>";
}

$templater = vB_Template::create('BAMFG_vehicle');
$templater->register('navbar', $navbar);
$templater->register('output', $output);
print_output($templater->render());

When the template is displayed, the table looks good and the links work well with the URL being listed as:
http://emycrosoft.com/BAMFG/bamfg_vehicle.php?do=delete_vehicle&id=42
when I click 'DELETE' on vehicle id 42, or whichever other vehicle I click on appears in the url respectively.

What I'm needing to do is pass the vehicle_id variable from the above code to:
if($_REQUEST['do'] == 'delete_vehicle') {
echo "HOLY CRAP - DELETING VEHICLE";



echo $id;

$templater = vB_Template::create('BAMFG_delete');

$templater->register_page_templates();
$templater->register('navbar', $navbar);
$templater->register('vehicle_id', $vehicle_id);
print_output($templater->render());}


I need the variable passed so I can do another query to pull the rest of the data from the table based upon the vehicle_id for a pop up confirmation I will be working on.

Should I rework the output from the original query to have the links rendered within the template so I an use the $POST command to resubmit the data? and if so, how? I'm currently googling my brains out looking for help!

So, in a nutshell this is the beginning and I've been putting this together through examples and tutorials I've found. I was hoping to get it working then go back through and 'streamline' and get rid of all that is not needed.

Thanks guys..

--------------- Added 1325390971 at 1325390971 ---------------

Parts of the code are not being used yet, as I'm working to through them one by one to get them working..

Here is the full .php file I'm working out of.. remember.. be kind =/
<?php

// ####################### SET PHP ENVIRONMENT ###########################
error_reporting(E_ALL & ~E_NOTICE);

// #################### DEFINE IMPORTANT CONSTANTS #######################

define('THIS_SCRIPT', 'test');
define('CSRF_PROTECTION', true);
// change this depending on your filename

// ################### PRE-CACHE TEMPLATES AND DATA ######################
// get special phrase groups
$phrasegroups = array();

// get special data templates from the datastore
$specialtemplates = array();

// pre-cache templates used by all actions
$globaltemplates = array();

// pre-cache templates used by specific actions
$actiontemplates = array();

// ######################### REQUIRE BACK-END ############################
require_once('./global.php');

// ################################################## #####################
// ######################## START MAIN SCRIPT ############################
// ################################################## #####################


$navbar = render_navbar_template($navbits);

// ###### YOUR CUSTOM CODE GOES HERE #####


// ###### NOW YOUR TEMPLATE IS BEING RENDERED ######


// **************** ADD VEHICLE ****************
if($_REQUEST['do'] == 'addvehicle') {


$templater = vB_Template::create('BAMFG_addvehicle');

$templater->register_page_templates();
$templater->register('navbar', $navbar);
$templater->register('addvehicle', $addvehicle);
print_output($templater->render());
}


// **************** DELETE VEHICLE ****************
// **************** DELETE VEHICLE ****************
// **************** DELETE VEHICLE ****************
if($_REQUEST['do'] == 'delete_vehicle') {
echo "HOLY CRAP - DELETING VEHICLE";



echo $id;

$templater = vB_Template::create('BAMFG_delete');

$templater->register_page_templates();
$templater->register('navbar', $navbar);
$templater->register('vehicle_id', $vehicle_id);
print_output($templater->render());


}

// **************** SAVE NEW VEHICLE ****************
// **************** SAVE NEW VEHICLE ****************
// **************** SAVE NEW VEHICLE ****************
if($_REQUEST['do'] == 'save_new_vehicle') {

$results = $db->query_first("SELECT count(*) as cnt FROM " . TABLE_PREFIX . BAMFG_vehicle);
$vehicle_id_plus = $results['cnt'];
$vehicle_id_plus = $vehicle_id_plus + 1;

echo "$vehicle_id_plus";

$year = $_POST['year'];
$make_model = $_POST['make_model'];
$interior = $_POST['interior'];
$exterior = $_POST['exterior'];
$audio_video = $_POST['audiovideo'];
$wheels_tires = $_POST['wheelstires'];
$suspension = $_POST['suspension'];
$drivetrain = $_POST['drivetrain'];

$sql = "INSERT INTO ". TABLE_PREFIX ."BAMFG_vehicle (
vehicle_id,
userid,
year,
make_model,
interior,
exterior,
audio_video,
wheels_tires,
suspension,
drivetrain) VALUES (
'". $vehicle_id_plus ."',
'". $userid ."',
'". $year ."',
'". $make_model ."',
'". $interior ."',
'". $exterior ."',
'". $audio_video ."',
'". $wheels_tires ."',
'". $suspension ."',
'". $drivetrain ."')";
$db->query_write($sql);
}


// **************** EDIT VEHICLE ****************
if($_REQUEST['do'] == 'edit_vehicle') {

echo "HOLY CRAP - I'm EDITING A VEHICLE";

$templater = vB_Template::create('BAMFG_edit');

$templater->register_page_templates();
$templater->register('navbar', $navbar);
$templater->register('vehicle_id', $vehicle_id);
print_output($templater->render());

}



// **************** VIEW VEHICLE ****************







$query = "SELECT * FROM BAMFG_vehicle";
$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result)){
$output .= "<tr><td width=100>".$row['year']."</td><td width=100>".$row['make_model']."</td><td width=20>".$row['vehicle_id']."</td><td width=100><a href='bamfg_vehicle.php?do=delete_vehicle&id=".$row['vehicle_id']."'>DELETE</a></td><td width=100><a href='bamfg_vehicle.php?do=edit_vehicle&id=".$row['vehicle_id']."'>EDIT</a></td></tr>";
}






$templater = vB_Template::create('BAMFG_vehicle');
$templater->register_page_templates();
$templater->register('pagetitle', 'Test Page');
$templater->register('navbar', $navbar);
$templater->register('test', $test);
$templater->register('output', $output);
$templater->register('my_array', $my_array);
$templater->register('test_bit_rendered', $test_bit_rendered);
$templater->register('loop_bit_rendered', $loop_bit_rendered);
print_output($templater->render());

?>

--------------- Added 1325391107 at 1325391107 ---------------

BAMFG_vehicle template:
{vb:stylevar htmldoctype}
<html xmlns="http://www.w3.org/1999/xhtml" dir="{vb:stylevar textdirection}" lang="{vb:stylevar languagecode}" id="vbulletin_html">
<head>
<title>{vb:raw vboptions.bbtitle} - {vb:raw pagetitle}</title>
{vb:raw headinclude}
{vb:raw headinclude_bottom}
</head>
<body>

{vb:raw header}

{vb:raw navbar}

<div id="pagetitle">
<h1>{vb:raw pagetitle}</h1>
</div>

<h2 class="blockhead">Title</h2>
<div class="blockbody">
<div class="blockrow">


VIEW VEHICLE TEMPLATE


<table>
{vb:raw output}
<tr><td><center><a href="bamfg_vehicle.php?do=addvehicle">Add New Vehicle</a></center></td></tr>
</table>





</div>
</div>

{vb:raw footer}
</body>
</html>

BAMFG_delete template
{vb:stylevar htmldoctype}
<html xmlns="http://www.w3.org/1999/xhtml" dir="{vb:stylevar textdirection}" lang="{vb:stylevar languagecode}" id="vbulletin_html">
<head>
<title>{vb:raw vboptions.bbtitle} - {vb:raw pagetitle}</title>
{vb:raw headinclude}
{vb:raw headinclude_bottom}
</head>
<body>

{vb:raw header}

{vb:raw navbar}

<div id="pagetitle">
<h1>{vb:raw pagetitle}</h1>
</div>

<h2 class="blockhead">Title</h2>
<div class="blockbody">
<div class="blockrow">



DELETE VEHICLE TEMPLATE

{vb:raw output}



<a href="bamfg_vehicle.php">CLICK TO VIEW VEHICLES</a>

</div>
</div>

{vb:raw footer}
</body>
</html>

BAMFG_edit template
{vb:stylevar htmldoctype}
<html xmlns="http://www.w3.org/1999/xhtml" dir="{vb:stylevar textdirection}" lang="{vb:stylevar languagecode}" id="vbulletin_html">
<head>
<title>{vb:raw vboptions.bbtitle} - {vb:raw pagetitle}</title>
{vb:raw headinclude}
{vb:raw headinclude_bottom}
</head>
<body>

{vb:raw header}

{vb:raw navbar}

<div id="pagetitle">
<h1>{vb:raw pagetitle}</h1>
</div>

<h2 class="blockhead">Title</h2>
<div class="blockbody">
<div class="blockrow">



EDIT VEHICLE TEMPLATE





<a href="bamfg_vehicle.php">CLICK TO VIEW VEHICLES</a>

</div>
</div>

{vb:raw footer}
</body>
</html>

kh99
01-01-2012, 03:47 AM
You don't have to change it to POST, just do something like this:

if($_REQUEST['do'] == 'delete_vehicle') {
echo "HOLY CRAP - DELETING VEHICLE";

$vbulletin->input->clean_gpc('r', 'id', TYPE_INT);
$id = $vbulletin->GPC['id'];


...and then next you'll probably want to make sure $id is actually set to a valid id, or at least is an integer >= the min id.