Problems, need help? Have a tip or advice? Post it here.
6 posts Page 1 of 1
I'm developing a literature request form on my company's website. Its an all-in-one PHP form that validates on the server side. I've hit a roadblock in development and was looking for some guidance.

Essentially I have couch looping our catalogs by folder or category. I'm collecting the literature quantities in the form and putting them into an array. I've hit a point where I need to be able to output the catalog name into the array variable in PHP. Here is the full code I'm using (trimmed down to show general function):

Code: Select all
<?php require_once( 'couch/cms.php' ); ?>
<?php
// Instatiate variables
$name = "";
$nameClass = "field";
$nameMessage = "";
$formtitle = "";
$formtitleClass = "field";
$formtitleMessage = "";
$company = "";
$companyClass = "field";
$companyMessage = "";
$address = "";
$addressClass = "field";
$addressMessage = "";
$address2 = "";
$city = "";
$cityClass = "field";
$cityMessage = "";
$state = "";
$stateClass = "field";
$stateMessage = "";
$zip = "";
$zipClass = "field";
$zipMessage = "";
$country = "";
$phone = "";
$phoneClass = "field";
$phoneMessage = "";
$fax = "";
$email = "";
$emailClass = "field";
$emailMessage = "";
$comments = "";
$pageMessage = "";
$content = "";

if(isset($_POST["submit"])) {

// Define variables
$name = stripslashes($_POST["name"]);
$formtitle = stripslashes($_POST["title"]);
$company = stripslashes($_POST["company"]);
$address = stripslashes($_POST["address"]);
$address2 = stripslashes($_POST["address2"]);
$city = stripslashes($_POST["city"]);
$state = stripslashes($_POST["state"]);
$zip = stripslashes($_POST["zip"]);
$country = stripslashes($_POST["country"]);
$phone = stripslashes($_POST["phone"]);
$fax = stripslashes($_POST["fax"]);
$email = stripslashes($_POST["email"]);
$comments = strip_tags($_POST["comments"]);
$comments = stripslashes($comments);
$error = 0;

// Validate name field
if(strlen($name) < 3) {
   $nameMessage = "<p>Please enter your name.</p>\r\n";
   $nameClass = "invalid";
   $error++;
} else {
   $nameClass = "field";
}

// Validate title field
if(strlen($formtitle) < 1) {
   $formtitleMessage = "<p>Please enter your job title.</p>";
   $formtitleClass = "invalid";
   $error++;
} else {
   $formtitleClass = "field";
}

// Validate company name
if (!preg_match('/^[a-z0-9 :,.!().?";&\'-]+$/i',$company) || empty($company)) {
   $companyMessage = "<p>Please enter your company name.</p>";
   $companyClass = "invalid";
   $error++;
} else {
   $companyClass = "field";
}

// Validate address
if (!preg_match('/^[a-z0-9 :#,.!().?";&\'-]+$/i',$address) || empty($address)) {
   $addressMessage = "<p>Please enter a valid address.</p>";
   $addressClass = "invalid";
   $error++;
} else {
   $addressClass = "field";
}

// Validate city
if (!preg_match("/^[a-zA-Z .]*$/",$city) || empty($city)) {
   $cityMessage = "<p>Please enter your city.</p>";
   $cityClass = "invalid";
   $error++;
} else {
   $cityClass = "field";
}

// Validate state
if (!preg_match("/^[a-zA-Z ]*$/",$state) || empty($state)) {
   $stateMessage = "<p>Please enter your State/Province.</p>";
   $stateClass = "invalid";
   $error++;
} else {
   $stateClass = "field";
}

// Validate zip
if (!preg_match("/^[a-zA-Z0-9 ]*$/",$zip) || empty($zip)) {
   $zipMessage = "<p>Please enter your Zip/Postal Code.</p>";
   $zipClass = "invalid";
   $error++;
} else {
   $zipClass = "field";
}

// Validate phone
if (!preg_match("/^(?!(?:\d*-){5,})(?!(?:\d* ){5,})\+?[\d- ]+$/",$phone) || empty($phone)) {
   $phoneMessage = "<p>Please enter a valid work phone number<br />(Only numbers, dashes, or spaces are accepted).</p>";
   $phoneClass = "invalid";
   $error++;
} else {
   $phoneClass = "field";
}

// Validate email
if (!preg_match("/^([a-z0-9_\.-]+\@[\da-z\.-]+\.[a-z\.]{2,6})$/i",$email) || empty($email)) {
   $emailMessage = "<p>Please enter a valid e-mail address.</p>";
   $emailClass = "invalid";
   $error++;
} else {
   $emailClass = "field";
}

//Find out how many elements are in this array
$total_num = count($_POST['catalog_qty']);

// These variables count how many total catalogs are in each section.
$numGen = $_POST["numGen"];
$numTms = $_POST["numTms"];
$numAcc = $_POST["numAcc"];
//Instatiate array
$catalog_names = array();

// Push catalog names into array for General
$i=0;
$key = $i+1;
while($i < $numGen) {
   array_push($catalog_names, $_POST["gen$key"]);
   
   ++$key;
   ++$i;
}
// Push catalog names into array for TMS
$i=0;
$key = $i+1;
while($i < $numTms) {
   array_push($catalog_names, $_POST["tms$key"]);
   ++$key;
   ++$i;
}
// Push catalog names into array for Accessories
$i=0;
$key = $i+1;
while($i < $numAcc) {
   array_push($catalog_names, $_POST["acc$key"]);
   ++$key;
   ++$i;
}

// Create a new array named order. Take the array values from the array catalog_names and make them the array keys, and take the user input for catalog_qty and make it the array elements.
$order = array_combine($catalog_names, $_POST["catalog_qty"]);

// Output a table with the array keys and values as the content of an e-mail that will be sent to us.
$content .= "<h1 style='font-weight:bold;font-size:1.3em;color:rgb(0,94,184);font-family:\"Arial\";'>Literature Request</h1>";
$content .= "<hr align='left' style='width:95%;height:5px;color:#A7A9AC;background:#A7A9AC;border:1px solid #A7A9AC;'/>";
$content .= "<p style='font-family:\"Arial\";'><strong>$company</strong><br />$name<br />$formtitle<br />$address";
if(!empty($address2)) {
   $content .= ", $address2<br />";
} else {
   $content .= "<br />";
}
$content .= "$city, $state $zip<br />$country<br />P: $phone";
if(!empty($fax)){
   $content .= "<br />F: $fax<br />";
} else {
   $content .= "<br />";
}
$content .= "Email: <a href='mailto:$email'>$email</a></p>";
if(!empty($comments)) {
   $content .= "<p style=\"font-family: 'Arial';font-size:12pt;\">\" $comments \"</p><br />";
}
$content .= "<table style='width:50%;font-family: \"Arial\";text-align:left;border-collapse:collapse;'>\r\n";
$content .= "<tr><th style='background:#005EB8;color:#FFF;padding:5px 0 5px 0;border:1px solid #A7A9AC;text-align:center;'>Catalog</th><th style='background:#005EB8;color:#FFF;padding:5px 0 5px 0;border:1px solid #A7A9AC;text-align:center;'>Qty</th></tr>";
$inc = 1;

foreach($order as $key => $value) {
   //Only create a table row for the catalogs that the user entered a qty for.
   
   if($value !== "") {
   $content .= "<tr><td style='padding:5px 0 5px 5px;border:1px solid #A7A9AC;'>$key</td><td style='padding:5px 0 5px 0;border:1px solid #A7A9AC;text-align:center;'>$value</td></tr>\r\n";
      
      // Get total amount of catalogs ordered
      if($value !== "") {
         $total = $total + $value;
      }
      
   }
}
$content .= "<tr><td style='text-align:right;font-weight:bold;border:1px solid #A7A9AC;padding-right:5px;'>Total:</td><td style='padding:5px 0 5px 0;border:1px solid #A7A9AC;text-align:center;'>$total</td></tr>\r\n";
$content .= "</table><br />";
$content .= "<p style='font-size:6pt;font-family:\"Arial\";'>[Disclaimer message]</p>";

   // Check validation for errors
   if($error > 0) { // Errors are present. Alert the user.
      if($error < 2) {
         $plural = "is $error field that needs";
      } else {
         $plural = "are $error fields that need";
      }
      $pageMessage = "<p class='redMessage'>There $plural your attention before proceeding.</p>\r\n";
   } else {
      // Make sure the user enter a value for at least 1 catalog.
      if($total < 1) {
         // No quantities entered for any catalogs. Communicate this issue to the user.
         $pageMessage = "<p class='redMessage'>Please enter a numeric quantity for each catalog you are interested in ordering.</p>\r\n";
      } else {
         // User entered a numerical quantity for at least one catalog. Execute the request by sending an email to sales.
         
         // Send us an e-mail with the literature request
         require_once("mail/PHPMailerAutoload.php");
         $mail = new PHPMailer;
         $mail->isSMTP();
         // Set PHPMailer to use the sendmail transport
         $mail->Debugoutput = 'html';
         $mail->Host = "host";
         $mail->Port = port;
         $mail->SMTPAuth = true;
         $mail->SMTPSecure = 'ssl';
         $mail->Username = "username";
         $mail->Password = "password";
         //Set who the message is to be sent from
         $mail->setFrom('from@email.com', 'From');
         //Set an alternative reply-to address
         $mail->addReplyTo('reply@email.com', 'Reply');
         //Set who the message is to be sent to
         $mail->addAddress('to@email.com', 'To');
         // Set blind copies
         $mail->addBCC('blindcopy@email.com');
         //Set the subject line
         $mail->Subject = 'Literature Request';
         $mail->Body = $content;
         $mail->IsHTML(true);
         //send the message, check for errors while sending the email
         if ($mail->send()) { */
             $pageMessage = "<p class='greenMessage'>Thank you $name! Your request has been received and will be shipping out within 1-2 weeks. We will contact you if we require any further information.</p>\r\n";
            // Clear variables and reset styles
            $name = "";
            $nameClass = "field";
            $formtitle = "";
            $formtitleClass = "field";
            $company = "";
            $companyClass = "field";
            $address = "";
            $addressClass = "field";
            $address2 = "";
            $city = "";
            $cityClass = "field";
            $state = "";
            $stateClass = "field";
            $zip = "";
            $zipClass = "field";
            $country = "";
            $countryClass = "field";
            $phone = "";
            $phoneClass = "field";
            $fax = "";
            $email = "";
            $emailClass = "field";
            $comments = "";
            
         } else {
             $err_message = "<p class='redMessage'>We're very sorry about this, but something went wrong when attempting to process your request. Please notify <a href='mailto:email@email.com'>email@email.com</a> so that we can correct this issue.</p>\r\n";
         }
      }
   }
}
?>
<?php $description = "Literature requests. Fill in the form to request any and all literature regarding tooling from us";?>
<?php $title = "Literature Request";?>
<?php $keywords = "literature, request, catalogs, flyers, brochures, lit, pdfs";?>
<?php include_once('include/header-secure.php');?>
<cms:template title='Literature Request Test' hidden='1'></cms:template> 
<link rel="alternate" hreflang="es" href="[Spanish equivalent]">
<style type="text/css">
.redMessage {color:rgba(0,0,0,1);font-size:10pt;font-family:'Arial';background: rgba(255,204,203,0.5);padding:20px;border:1px solid rgba(255,117,113,1);margin-bottom:15px;}
.greenMessage {font-size:11pt;font-family:'Arial';background: rgba(0,178,0,0.05);padding:20px;border:1px solid rgba(0,178,0,0.5);}
.catalog {float: left;margin: 10px;padding: 10px;width: 200px;height: 275px;border: 1px solid rgba(167,169,172,0.5);text-align:center;background:rgba(0,94,184,0.1);font-family:'Abel', 'Arial';}
.catalog:hover {background:rgba(255,159,0,0.1);border: 1px solid rgba(255,159,0,0.5);}
.catalog img {border:1px solid rgba(167,169,172,1);margin:0 auto;}
.catalog img:hover {border:1px solid rgba(255,159,0,1);}
.catalog a {text-decoration:none;}
.cat-name {height:35px;font-family:'Arial';font-size:10.5pt;color:rgba(88,89,91,1);margin-bottom:5px;}
a.online-only {color:rgba(187,41,0,1);font-family:'Arial';font-size:10pt;}
a.online-only:visited {color:rgba(187,41,0,1);}
a.online-only:hover {text-decoration:underline;}
hr.gray {height: 5px;color: #A7A9AC;background: #A7A9AC;border: 1px solid #A7A9AC;}
h2 {font-size:20pt;padding-bottom:25px;}
textarea {font-family:"Arial";font-size:11pt;padding:10px;width:85%;height:75px;overflow:auto;resize:none;}
input[type="tel"].qty {font-family:'Arial';text-align:center;width:25px;border:1px solid rgba(167,169,172,1);font-size:11pt;padding:3px 5px 3px 5px;}
input[type="text"].field {font-family: 'Arial';font-size:11pt;border:1px solid #CCCCCC;padding:3px 5px 3px 5px;width:200px;}
input[type="tel"].field {font-family: 'Arial';font-size:11pt;border:1px solid #CCCCCC;padding:3px 5px 3px 5px;width:200px;}
input[type="email"].field {font-family: 'Arial';font-size:11pt;border:1px solid #CCCCCC;padding:3px 5px 3px 5px;width:200px;}
input[type="text"].invalid {border:1px solid rgba(255,117,113,1);background:rgba(255,204,203,0.5);font-family: 'Arial';font-size:11pt;padding:3px 5px 3px 5px;width:200px;}
input[type="tel"].invalid {border:1px solid rgba(255,117,113,1);background:rgba(255,204,203,0.5);font-family: 'Arial';font-size:11pt;padding:3px 5px 3px 5px;width:200px;}
input[type="email"].invalid {border:1px solid rgba(255,117,113,1);background:rgba(255,204,203,0.5);font-family: 'Arial';font-size:11pt;padding:3px 5px 3px 5px;width:200px;}
input.submit[type="submit"] {font-family: 'Abel', 'Arial';letter-spacing: 1px;font-size: 11pt;background: rgba(0,94,184,1);color: #FFF;padding: 3px 10px;border-radius: 15px;border: medium none;}
input.submit[type="submit"]:hover {background: rgba(255,159,0,1);cursor:pointer;}
</style>
</head>
<?php include_once('include/nav-top-secure.php'); ?>
<?php include_once('include/nav-secure.php');?>
<?php include_once('include/sub-nav-requests.php');?>
<div class="container">
   <div class="row">
      <div class="twelvecol last">
         <p class="breadcrumbs"> <a href="../requests.php">Requests</a> >> Literature Request</p>
      </div>
   </div>
</div>
<div class="container">
   <div class="row">
      <div class="twelvecol last">
         <?php echo $content;?>
         <?php echo $pageMessage; ?>
         <h1>Literature Request</h1><hr><br />
      </div>
   </div> <!-- Close row -->
   <div class="row">
      <div class="twelvecol last">
         <h2>Thank you for your interest in [Company Name].<br />
            <br />
            Please enter your information:</h2>
      </div>
   </div> <!-- Close row -->
   <form action="[submit to self]" method="POST">
      <div class="row">
         <div class="sixcol"> <!-- Left column-->
            <div class="form-title">Name:</div>
            <div class="form-title"><input name="name" type="text" class="<?php echo $nameClass; ?>" value="<?php echo $name; ?>" /><?php echo $nameMessage; ?></div><br class="clear">
            <div class="form-title">Title:</div>
            <div class="form-title"><input name="title" type="text" class="<?php echo $formtitleClass; ?>" value="<?php echo $formtitle; ?>" /><?php echo $formtitleMessage; ?></div><br class="clear">
            <div class="form-title">Company Name:</div>
            <div class="form-title"><input name="company" type="text" class="<?php echo $companyClass; ?>" value="<?php echo $company; ?>" /><?php echo $companyMessage; ?></div><br class="clear">
            <div class="form-title">Address:</div>
            <div class="form-title"><input type="text" name="address" class="<?php echo $addressClass; ?>" value="<?php echo $address; ?>" /><?php echo $addressMessage; ?></div><br class="clear">
            <div class="form-title">Address 2:</div>
            <div class="form-title"><input name="address2" type="text" class="field" value="<?php echo $address2; ?>" /></div><br class="clear">
            <div class="form-title">City:</div>
            <div class="form-title"><input name="city" type="text" class="<?php echo $cityClass; ?>" value="<?php echo $city; ?>" /><?php echo $cityMessage; ?></div><br class="clear">
         </div> <!-- Close left column-->
         <div class="sixcol last"> <!-- Right column -->
            <div class="form-title">State/Province:</div>
            <div class="form-title"><input name="state" type="text" class="<?php echo $stateClass; ?>" value="<?php echo $state; ?>" /><?php echo $stateMessage; ?></div><br class="clear">
            <div class="form-title">Zip/Postal Code:</div>
            <div class="form-title"><input name="zip" type="tel" class="<?php echo $zipClass; ?>" value="<?php echo $zip; ?>" /><?php echo $zipMessage; ?></div><br class="clear">
            <div class="form-title">Country:</div>
            <div class="form-title"><input name="country" type="text" class="<?php echo $countryClass; ?>" value="<?php echo $country; ?>" /><?php echo $countryMessage; ?></div><br class="clear">
            <div class="form-title">Work Phone:</div>
            <div class="form-title"><input name="phone" type="tel" class="<?php echo $phoneClass; ?>" value="<?php echo $phone; ?>" /><?php echo $phoneMessage; ?></div><br class="clear">
            <div class="form-title">Fax:</div>
            <div class="form-title"><input name="fax" type="tel" class="field" value="<?php echo $fax; ?>" /></div><br class="clear">
            <div class="form-title">Email:</div>
            <div class="form-title"><input name="email" type="email" class="<?php echo $emailClass; ?>" value="<?php echo $email; ?>" /><?php echo $emailMessage; ?></div><br class="clear">
         </div> <!-- Close right column-->
      </div> <!-- Close row -->
      <div class="row">
         <div class="twelvecol last">
            Comments:<br />
            <textarea placeholder="Add any additional comments here" name="comments"><?php echo $comments;?></textarea>
         </div>
      </div> <!-- Close row -->
      <div class="row"><br />
         <h2>Please enter your desired literature quantities:</h2>
         <p class="gold bold">Note: You can click on each image to view or download the PDF version.</p>
      </div> <!-- Close row -->
      <div class="row">
         <div class="twelvecol last"> <!-- Open General -->
            <h3>General</h3>
         </div><br /> <!-- Close twelvecol-->
         <cms:set my_counter="1" />
         <cms:pages masterpage='product-pdfs.php' folder='general' order='asc'>
         <cms:if pdf_only = 'printed' >
         <div class="catalog">
            <div class="cat-name"><cms:show pdf_name /></div>
            <a href="<cms:show pdf_link />" target="_blank">
               <img src="<cms:show cat_thumb />" alt="<cms:show pdf_name />" />
            </a><br />
            <input type="hidden" name="numGen" value="<cms:show my_counter />" />
            <input type="hidden" name="gen<cms:show my_counter />" value="<cms:show pdf_name />" />
            Quantity: <input type="tel" name="catalog_qty[]" class="qty" maxlength="2" value="<?php echo $order['<cms:show pdf_name'];?>"/>
         </div> <!-- Close catalog-->
         <cms:incr my_counter />
         <cms:else />
            <cms:if pdf_only = 'Out of Stock'>
            <div class="catalog">
               <div class="cat-name"><cms:show pdf_name /></div>
               <a href="<cms:show pdf_link />" target="_blank">
                  <img src="<cms:show cat_thumb />" alt="<cms:show pdf_name />" />
               </a><br />
               <a href="<cms:show pdf_link />" target="_blank" class="online-only">
                  * Out of stock. Please check back soon. *
               </a>
            </div>
            <cms:else />
            <div class="catalog">
               <div class="cat-name"><cms:show pdf_name /></div>
               <a href="<cms:show pdf_link />" target="_blank">
                  <img src="<cms:show cat_thumb />" alt="<cms:show pdf_name />" />
               </a><br />
               <a href="<cms:show pdf_link />" target="_blank" class="online-only">
                  * Only Available Online *
               </a>
            </div>
            </cms:if>
         </cms:if>
         </cms:pages >
         <div class="clear"></div>
         <hr class="gray" /><br /><!-- End General -->
         
         <div class="twelvecol last"> <!-- Open Tool Presetters -->
            <h3>Tool Presetters</h3>
         </div><br /> <!-- Close twelvecol-->
         <cms:set my_counter="1" />
         <cms:pages masterpage='product-pdfs.php' folder='tool-presetters' order='asc'>
         <cms:if pdf_only = 'printed' >
         <div class="catalog">
            <div class="cat-name"><cms:show pdf_name /></div>
            <a href="<cms:show pdf_link />" target="_blank">
               <img src="<cms:show cat_thumb />" alt="<cms:show pdf_name />" />
            </a><br />
            <input type="hidden" name="numTms" value="<cms:show my_counter />" />
            <input type="hidden" name="tms<cms:show my_counter />" value="<cms:show pdf_name />" />
            Quantity: <input type="tel" name="catalog_qty[]" class="qty" maxlength="2" value="<?php echo $order['<cms:show pdf_name'];?>"/>
         </div> <!-- Close catalog-->
         <cms:incr my_counter />
         <cms:else />
            <cms:if pdf_only = 'Out of Stock'>
            <div class="catalog">
               <div class="cat-name"><cms:show pdf_name /></div>
               <a href="<cms:show pdf_link />" target="_blank">
                  <img src="<cms:show cat_thumb />" alt="<cms:show pdf_name />" />
               </a><br />
               <a href="<cms:show pdf_link />" target="_blank" class="online-only">
                  * Out of stock. Please check back soon. *
               </a>
            </div>
            <cms:else />
            <div class="catalog">
               <div class="cat-name"><cms:show pdf_name /></div>
               <a href="<cms:show pdf_link />" target="_blank">
                  <img src="<cms:show cat_thumb />" alt="<cms:show pdf_name />" />
               </a><br />
               <a href="<cms:show pdf_link />" target="_blank" class="online-only">
                  * Only Available Online *
               </a>
            </div>
            </cms:if>
         </cms:if>
         </cms:pages >
         <div class="clear"></div>
         <hr class="gray" /><br /><!-- Close Tool Presetters -->
         <div class="twelvecol last"> <!-- Open Accessories -->
            <h3>Accessories</h3>
         </div><br /> <!-- Close twelvecol-->
         <cms:set my_counter="1" />
         <cms:pages masterpage='product-pdfs.php' folder='accessories' order='asc'>
         <cms:if pdf_only = 'printed' >
         <div class="catalog">
            <div class="cat-name"><cms:show pdf_name /></div>
            <a href="<cms:show pdf_link />" target="_blank">
               <img src="<cms:show cat_thumb />" alt="<cms:show pdf_name />" />
            </a><br />
            <input type="hidden" name="numAcc" value="<cms:show my_counter />" />
            <input type="hidden" name="acc<cms:show my_counter />" value="<cms:show pdf_name />" />
            Quantity: <input type="tel" name="catalog_qty[]" class="qty" maxlength="2" value="<?php echo $order['<cms:show pdf_name'];?>"/>
         </div> <!-- Close catalog-->
         <cms:incr my_counter />
         <cms:else />
            <cms:if pdf_only = 'Out of Stock'>
            <div class="catalog">
               <div class="cat-name"><cms:show pdf_name /></div>
               <a href="<cms:show pdf_link />" target="_blank">
                  <img src="<cms:show cat_thumb />" alt="<cms:show pdf_name />" />
               </a><br />
               <a href="<cms:show pdf_link />" target="_blank" class="online-only">
                  * Out of stock. Please check back soon. *
               </a>
            </div>
            <cms:else />
            <div class="catalog">
               <div class="cat-name"><cms:show pdf_name /></div>
               <a href="<cms:show pdf_link />" target="_blank">
                  <img src="<cms:show cat_thumb />" alt="<cms:show pdf_name />" />
               </a><br />
               <a href="<cms:show pdf_link />" target="_blank" class="online-only">
                  * Only Available Online *
               </a>
            </div>
            </cms:if>
         </cms:if>
         </cms:pages >
         <div class="clear"></div>
         <hr class="gray" /><br /><!-- Close Accessories -->
      </div> <!-- Close row -->
      <div class="row">
         <input type="submit" value="Send Me Literature!" name="submit" class="submit" />
      </div> <!-- Close row -->
   </form>
   <div class="bottom-bumper"></div>
</div> <!-- Close container -->
<?php include_once('include/footer-secure.php');?>
</body>
</html>
<?php COUCH::invoke(); ?>


I'm interested in getting couch to output the catalog names as the key for the array variable order. You can see it in the code as
Code: Select all
value="<?php echo $order['<cms:show pdf_name />'];?>"
This way if the form has invalid fields, the user won't lose the quantities that they've already entered. We've all had this happen when filling out a form online, and it is not a good user experience.

What are my options?
There is an order of operations between Couch and PHP. Regular old PHP tags
Code: Select all
<?php echo 'something'; ?>
are executed first, before Couch is instantiated.

PHP inside of the Couch tag
Code: Select all
<cms:php>echo 'something';</cms:php>
is executed after Couch is up and running and therefore has access to all of the Couch variables. For this reason, it's generally better to use the <cms:php> tag, even though it's probably less familiar to you.
Code: Select all
<cms:php>echo "<cms:show my_variable />";</cms:php>

To make a variable created in PHP into a Couch variable, there is a Couch function:
Code: Select all
$CTX->set( 'my_couch_variable', $my_php_variable );
Now the variable generated by your PHP code can be used in your Couch code.

To go in the other direction, use $CTX->get().
Code: Select all
$my_php_variable = $CTX->get( 'my_couch_variable' );


I admit that my eyes glazed over while reading your code :), so I might be a little vague on your exact needs, but I hope these details about combining Couch and PHP give you the help you need. Please say so if I missed the point.
Thanks Tim! My apologies for the lengthy code, but I just wanted to post the whole "shabang" so that everyone would have the full story. My eyes glaze over when I look at it too. :lol:

You made some good suggestions. I'll try it out and let you know how it goes. Thanks again!
Hmmm. I'm not getting the function to work. Are these function used like this?

Code: Select all
<cms:php> $CTX->set( 'my_couch_variable', $name ); </cms:php>


I was trying to set it to any existing variable with a value to test it. Although it gives me the following error:

Fatal error: Call to a member function set() on a non-object


This code:
Code: Select all
<?php $CTX->set( 'my_couch_variable', $name ); ?>


...doesn't seem to set any variable couch. When I call it like so:

Code: Select all
<cms:show my_couch_variable />


Please excuse my ignorance if I'm doing this wrong. Like you said, I'm much more familiar with PHP syntax than these custom couch tags. I can get by using the standard couch tags, but I'm stumped in this situation.
Sorry. That's my mistake. I left out a part. Try
Code: Select all
<cms:php>
global $CTX;
$CTX->set( 'my_couch_variable', $name );
</cms:php>
No problem. Thanks for the follow-up. I tried it with the global variable as you pointed out, but unfortunately I couldn't get it to work.

I think I'm going to just make a separate SQL table with the information I need to pull and loop it traditionally outside of couch. Thanks again for your help. :)
6 posts Page 1 of 1