I am trying to convert a pre-existing site that had html and php intermingled into a Smarty template based site. I never used Smarty before so this is proving very difficult for me. I get that you can assign a variable like so:
$smarty->assign('number_of_items_in_cart', $number_of_items_in_cart);
and use it in the tpl file like so:
{$number_of_items_in_cart}
but what about more complex things like this block of code that I had on the old site:
$query = mysql_query(" SELECT * FROM products WHERE id = '$pid' ");
if (mysql_num_rows($query) == 1) {
while ($row = mysql_fetch_array($query)) {
extract($row);
echo "<h2>$name</h2>";
echo "<img src='images/product_images/$image' alt='' width='100' />";
echo $description;
echo '<p>'.money($price).'</p>';
echo "<input type='text' value='1' class='qty-$id' />";
echo "Add to Cart";
}
} else {
redirect('404.php');
}
How can I work with this in a Smarty template, since the output is within a while loop?
Instead of echoing this, you can append it in a string variable then pass it to smarty:
$string = "";
while ($row = mysql_fetch_array($query)) {
extract($row);
$string .= "<h2>$name</h2>";
$string .= "<img src='images/product_images/$image' alt='' width='100' />";
$string .= $description;
$string .= '<p>'.money($price).'</p>';
$string .= "<input type='text' value='1' class='qty-$id' />";
$string .= "Add to Cart";
}
Now u can pass it to smarty
$smarty->assign('place_holder', $string);
I hope this is what you are looking for
You can use the foreach builtin function to iterate over an array containing your query results.
You could also use foreachelse to display an alternate message (though in this case you're redirecting).
See example 7.9 in http://www.smarty.net/docsv2/en/language.function.foreach .
Edit: there's also a while function if that's what you really want.
Related
I have a function like this:
function remove_chars($string)
{
$string = #stripslashes($string);
$string = str_ireplace("'","'",$string);
$string = str_ireplace('"','"',$string);
return trim($string);
}
That I then use in the mySQL result like this below:
while($row = mysql_fetch_array($result)){
$name = remove_chars($row['name']);
// ...
echo "<td width=\"100%\"><input type=\"text\" name=\"name[]\" value=\"".$name."\"
style=\"width:100%;\" /></td>";
// ...
}
But that's not working. The special characters are still displaying and therefor also messing up the code. Any ideas why?
I am attempting to create an ordered list from a text file. As my code currently stands, it modifys the original text file with the input all on the same line(list number). e.g if I input "mercury" it will come out as 1. mercury, but if I input "venus", it will appear as 1.mercuryvenus
I am trying to get it to work so that if I input some text such as "mercury" and hit the submit button, it will appear as
1. mercury. If I input some more text such as "venus", it will appear as 2. venus, all in ordered list format. I assume that explode may be used for this, but I am unsure of how to implement this properly. Another option would be to create a new text file for each input if that were to be more efficient.
echo "<form method='post'>
<label>Enter some text</label><br>
<textarea name='textbox' cols='60' rows='5' required></textarea>
<br>
<input type='submit' name='submit' value='Submit'/>
<input type='hidden' name='step' value=''/>
</form>";
echo "<section>";
echo "<h3>Current tasks</h3>";
$text = ("text.txt");
$extract = (isset($_POST['textbox']) ? $_POST['textbox'] : null);
$file = fopen($text,"a");
fwrite($file,$extract);
fread($file,filesize("text.txt"));
fclose($file); #Not sure where this should really go
$c = array(file_get_contents('text.txt'));
$x = explode(" ",$c); #Could be wrong format
echo "<ol>";
foreach($c as $r) {
echo "<li>" . $r. "</li>", "<br>";
}
echo "</ol>";
echo "</section>";
Here is the solution
echo "<section>";
echo "<h3>Current tasks</h3>";
$text = "text.txt";
$extract = (isset($_POST['textbox']) ? $_POST['textbox'] : null);
$file = fopen($text,"a+");
fwrite($file," ".$extract);
#fread($file,filesize("$text"));
$x = explode(" ",file_get_contents($text));
if(isset($_POST['submit'])) {
echo "<ol>";
foreach ($x as $r) {
echo "<li>" . $r . "</li>", "<br>";
}
echo "</ol>";
echo "</section>"
First, the "a" in fopen($text,"a") means append. Which means if you already have the text "mercury" in your file and your run your program again with "venus", you will be appending "venus" on the end of "mercury" to get "mercuryvenus". If you want a space between the two, you will have to add it when your write it to file: fwrite($file, " ".$extract);
Second, you do $x = explode(... and then do not use $x in your foreach statement. Use $x instead of $c in your foreach.
I am trying to echo a array value in a link, but it is coming up in dreamweaver as an error, but I cant work out what I have done wrong, can anyone tell me what is wrong with this line please ?.
thanks :-)
echo '';
EDIT >>>>>>>>>>>>>>>>>>>>>>>>
THIS IS THE FULL CODE :
$result = mysql_query("SELECT * FROM hqfjt_chronoforms_data_addemailtemplate");
while ($row = mysql_fetch_object($result)) {
echo '<div class="namerow">';
echo '<th>';
echo $row->emailformname;
echo '</th>';
echo '</div>';
echo '<div class="messagerow">';
echo '<th>';
echo $row->emailformmessage;
echo 'dssd';
echo '<tr></tr>';
echo '</div>';
}
echo '</th>';
mysql_free_result($result);
If I echo the cf_uid
echo $row->cf_uid;
this works fine and displays the unique id for each record next to it in the table, I just need to take that id thats is being echo'd and put in at the end of the link so that it looks like http://link&token=2626382837728 << (cf_uid)
FIXED !
Thanks for everyone's help on this work, I worked out what was wrong in the end, what I thought was an array didn't appear to be, this code worked in the end >>
$result = mysql_query("SELECT * FROM hqfjt_chronoforms_data_addemailtemplate");
while ($row = mysql_fetch_object($result)) {
echo '<div class="namerow">';
echo '<th>';
echo $row->emailformname;
echo '</th>';
echo '</div>';
echo '<div class="messagerow">';
echo '<th>';
echo $row->emailformmessage;
$id = $row->cf_uid;
echo 'LINK';
echo '<tr></tr>';
echo '</div>';
}
echo '</th>';
mysql_free_result($result);
Try to separate the strings and the variables.
echo '';
update: If you get an error in this line the problem could be the line before!
alternatively you can try this
echo '';
This way you can just concat the string
echo "SOME NAME FOR THE LINK";
[edit based on updated post]
Use this:
echo 'dssd';
However, I must ask... is there an array variable $detail which has a key 'cf_uid'? And what syntax error are you getting (after you tried this)?
[edit based on comment]
Since it's $row and since it's an object:
echo 'dssd';
I have the following code:
while($row = mysql_fetch_array($result)){
$output_items[] = $row["title"]; } // while
print(implode("\n", $output_items));
Which does what it says and splits the array with a new line for each item.
But how do I do the same and allow formatting with i.e. I basically want to say
foreach of the $output_items echo "<div class=whatever>$output_items</div> etc etc
Tearing my hair out with this!
Many thanks for all help
Darren
foreach ($output_items as $oi){
echo "<div class=whatever>$oi</div>";
}
doesn't work? or i did not get what you are searching for
Pretty simple, to make it easier to read I'd do something like this:
while($row = mysql_fetch_array($result))
{
echo '<div class="whatever">';
echo $row["title"];
echo '</div>' . "\n";
} // while
Although you could still do this with your original code pretty easily:
while($row = mysql_fetch_array($result)){
$output_items[] = '<div class="whatever">' . $row["title"] . '</div>'; } // while
print(implode("\n", $output_items));
Rather than implode() them all with line breaks, use string interpolation to add them together:
$out_string = "";
// Loop over your array $output_items and wrap each in <div />
// while appending each to a single output string.
foreach ($output_items as $item) {
$out_string .= "<div class='whatever'>$item</div>\n";
}
echo $out_string;
Just got some help with my ajax/php search but now my issue is that the link is not even showing up on the search page. The echo results are showing up but the $string isn't.
Thanks for the help.
//echo $query;
$result = mysqli_query($link, $query);
$string = '';
if($result){
if(mysqli_affected_rows($link)!=0){
while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
echo '<p> <b>'.$row['title'].'</b> '.$row['post_ID'].'</p>' ;
$string .= "<p><a href='set-detail.php?recordID=".$row['post_ID']."'>".$row['title']."</a></p>";
}
} else {
echo 'No Results for :"'.$_GET['keyword'].'"';
}
I don't see where you echo $string. If you put the echo command in there for $string it may begin to work.
You're not printing $string anywhere.
Are you sure you meant to use $string, and not just echo it, like you have with the line above?
You need to include echo $string; or simple echo the line as it is generated if you are calling this inline i.e.
echo "<p><a href='set-detail.php?recordID=".$row['post_ID']."'>".$row['title']."</a></p>";
You do not echo the string above.
After your loop you need to echo it.
if(mysqli_affected_rows($link)!=0){
while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)){
echo '<p> <b>'.$row['title'].'</b> '.$row['post_ID'].'</p>' ;
$string .= "<p><a href='set-detail.php?recordID=".$row['post_ID']."'>".$row['title']."</a></p>";
}
echo $string;