In the following code when I try to assign $viewAd value to the template file and display result it does not display the accurate results when assigned to the template. However, it displays the accurate desired result on top of the page when I straight echo the $viewAd in the PHP page. I have given the screenshots below.
My PHP Structure goes like this:
$cat = $pdo->prepare("SELECT QUERY HERE");
$cat-> execute();
while($s = $cat->fetch()){
$ads = $pdo->prepare("ANOTHER SELECT QUERY HERE");
$ads-> execute();
$ads_count = $ads->rowCount();
if($ads_count > 0){
$viewAd = "<h4>".$s['pcat_category']."</h4>"; // Echoing Category Name
while($a = $ads->fetch()){
if(isLoggedIn()){
// If logged in display Ads relevant to members
$viewAd .= 'SOME HTML DATA';
foreach($membershipData as $mbs){
$viewAd .= 'EXTENDED HTML DATA';
}
$viewAd .= 'CLOSING HTML DATA';
}else{
// If not logged in display ads relevant to outsiders
$viewAd .= 'SOME HTML DATA';
foreach($membershipData as $mbs){
$viewAd .= 'EXTENDED HTML DATA';
}
$viewAd .= 'CLOSING HTML DATA';
}
}
echo $viewAd; // RETURNS DESIRED RESULT ABOVE THE TEMPLATE ON TOP
}
}
$smarty->assign('viewAds', $viewAd); // ASSIGNED TO TEMPLATE BUT DOES NOT RETURN DESIRED RESULT
Screenshot 1 : Correct Result with straight PHP echo displays each category with ads in it
Screenshot 2 : Incorrect Result when variable assigned to the template displays only the last category with the ads in it
Why am I not getting the same result when I assign the variable to the template? I have also tried the array method where I declared $viewAds = array() before the while loop and later I assigned $viewAds[] = $viewAd. Then in the tpl file I tried to echo the value using foreach loop like this {foreach $viewAds as $vas}{$vas}{/foreach} but that still didn't display each category and ads (the desired result like in the first screenshot). Since, it did not work using array as well I removed it and tried to echo {$viewAds} straight way and with foreach too. No luck, nothing is working. All gives the result as in second screenshot. Displaying only the last category with ad in it. However, since direct echo in PHP file is giving the correct result I am sure that my PHP logic is correct. It's just that I am not being able to assign that result in the template file correctly and display it. What is the error I am making here? Am I missing out on something?
You need to initialize $viewAd before starting the first loop, something like:
$viewAd = "";
Then each category should be appended to $viewAd :
$viewAd .= "<h4>".$s['pcat_category']."</h4>";
This way all categories will be included in your final HTML.
Related
I need to check if there is a product code in db else assign a product code starting with 101 and add 0001
I am trying like this
$store_code="101";
$product_code=($store_code(0001));
but I get a blank page ...
The result I need is 1010001
any sugestion is welcome
NOTE: the store code comes from MySQL but I show it now like this for an easier understanding
So if I get this correctly, you first check if the product code is in database, and you have no truble doing that.
//lets say $product_code is your code
//if code not in db:
$product_code .= "0001"; //this will add 0001 at the end of the $product_code variable
or
$product_code = $store_code . "0001";
I am trying to send an email in PHP where the content of the email has some conditional checks and some database query lookups.
What I would like to acheive is having my email code as a variable (similar to below) so that I can sent mail() the content to the relevant people.
$emailContent = "<p>My email content</p>";
However the value of this variable would have some code like this:
<table>
<?php
$get_course_units = "SELECT * FROM course_units where course_units.course_code = {$courseCodeExtract}";
$course_units_results = $conn->query($get_course_units);
if ($course_units_results->num_rows > 0) {
while ($courseUnits = $course_units_results->fetch_assoc()) {
?>
<tr>
<td><?php echo $courseUnits["unit_code"]; ?> – <?php echo $courseUnits["unit_name"]; ?> </td>
</tr>
<?php
} //end loop for course units
} //end if for course units
?>
</table>
How should I continue?
Split up your script into an html template file and your php logic.
Use shortcodes in your templates where you want to have custom information and then use str_replace to replace that content with the actual values.
$template_string = file_get_contents('myfile.html');
$shortcodes = array("{{FNAME}}","{{LNAME}}","{{OTHER_STUFF}}");
for(/* all the people you want to mail */){
$custom_info = get_custom_info(/* person */); //eg returns assoc array
$result = $template_string;
foreach($shortcodes as $code){
$result = str_replace($code, $custom_info[$code], $result);
}
//do what you want with result and mail it
}
In the example above, get_custom_info would be returning an associative array with the same values as the shortcodes array, just for convenience.
Now anywhere I put {{FNAME}} in my html, it will be replaced with the value I get back from the custom info function.
You can easily extend this to scrape the template and look for {{ and }} (or whatever shortcode syntax you want) anddetermine what variables you will need from your custom info, shaping the query to only give you what you actually need.
Not sure if this is the best way, but it seems to work pretty well. (also best way is subjective, so might want to ask questions a little differently)
This may not be enough information for anyone to answer this, but I might just be missing something y'all would know by looking at the code.
In my site's admin panel, I can assign custom fields to categories (No. of beds, baths, whatever I create), but in order for the fields to show up on the Advanced Search page, you have to assign the field(s) to all of my sites categories.
I am trying to get the field(s) to show up all the time, even if they are only assigned to certain categories, not all of them.
Here is the code that renders the fields on the Advanced Search page, but again, only renders them if the field is assigned to all categories:
<?php
$get_catID = get_CATID($_GET['ad_cat_cat']);
if(empty($get_catID)) $get_catID = 0;
$get_catID = array($get_catID);
$arr = get_category_fields_without_vals($get_catID, 'no');
for($i=0;$i<count($arr);$i++)
{
echo '<tr>';
echo '<td>'.$arr[$i]['field_name'].$arr[$i]['id'].':</td>';
echo '<td>'.$arr[$i]['value'].'</td>';
echo '</tr>';
}
?>
I am currently working on website where it has a search box to search for specific items. The page echos out results in table format. So far everything works perfectly but when I try to filter the result(depending on features), I get two sets of results. One with previously displayed result table and the other is the filtered result. I do not want previous result to display back again on screen without affecting any other procedure. Something like sessions?? I do not know exactly how to deal with this situation.
<?php
include'search.php';// form for a search box.
if (isset($_POST['search_name'])) {
$search_name=mysql_real_escape_string(htmlentities(trim($_POST['search_name'])));
$errors = array();
if (empty($search_name)){
$errors[] ='please enter a search term';
}
else if (strlen($search_name)<3){
$errors[] = 'your search term must be three or more characters';
}
else if (1==2){
$errors[] ='your search for '.$search_name.' returened no results';
}
if (empty($errors)){
filter($search_name); //it display another form in the navigation bar to filter the search result.
search_results($search_name);//searches for all the result onthe database depending on the keyword entered in searchbox.
} else{
foreach($errors as $error) {
echo $error,'</br>';
}
}
}
?>
See this code:
echo 'world';
echo 'hello !';
You can intercept the echo using ob_start(), ob_get_contents() and ob_clean().
ob_start();
echo 'world';
var $echoed = ob_get_contents();
ob_clean();
// real echo
echo 'hello ' . $echoed . '!';
// now you see
// hello world!
Because the ob 'output buffering' is native to PHP, you can use it with anything like functions, includes and so on. I'm using this approach, to intercept (1.) outputs in my controller flow, and to intercept (2.) the view's output, so I can compose them later (for example to render PHP errors into a debug div.
I've built a contest system where users submit tickets then one is randomly chosen to win, and now I'm trying to figure out a way to display to users the tickets they have already submitted. Each ticket has an id, a date, and an invoicenumber. I want to display all the invoice numbers that a user has submitted so far.
Here is the method I have in my methods page. (I've organized my methods into one php file and then i just call them when needed.)
function GetSubmittedBallots()
{
if(!$this->CheckLogin())
{
$this->HandleError("Not logged in!");
return false;
}
$user_rec = array();
if(!$this->GetUserFromEmail($this->UserEmail(),$user_rec))
{
return false;
}
$qry = "SELECT invoicenumber FROM entries WHERE user_id = '".$user_rec['id_user']."'";
$result = mysql_query($qry,$this->connection);
while($row = mysql_fetch_array($result))
{
echo $row['invoicenumber'];
}
}
and then on my html page that I want it to echo on, i just call it
<?php GetSubmittedBallots(); ?>
Sadly, this doesn't work. So my question is, how would i go about displaying the $row array on my html page?
<?php
require("methods.php"); // Include the file which has the "GetSubmittedBallots" function or method, if it's in a separate file
GetSubmittedBallots(); // Run the function / method
?>
If this doesn't work, please let us know any errors you receive.
Does it echo "Array"?
That's because you are trying to echo an array.
You should use something like print_r or var_dump, given that you are just trying to access the queried results. In my opinion the method should build a multidimensional array with the records, and then the template logic should loop through them and echo the values in a nice way. Be it a table or nicely arranged HTML.
If I'm not wrong, $this keyword is indicating you're in a class? If so, you need first to init that class and try to call GetSubmittedBallots function after init;
// assuming that class's name is Users
$users = new Users();
$users->GetSubmittedBallots();