Smarty php variables output - php

Using realestate script 3 which uses smarty 3.
I managed to create the loop which gets information from the database.
<?php
function smarty_function_my_plugin($params,&$smarty)
{
$con=mysqli_connect("localhost","root","","res3");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
} else {
$result = mysqli_query($con,"SELECT * FROM res3_listings WHERE listing_type_id=5 ORDER BY views DESC");
while($row = mysqli_fetch_array($result)) {
$title=$row['title_1'];
$price=$row['price'] ;
$id=$row['listing_id'];
$result = mysqli_query($con,"SELECT listing_photo_file FROM res3_listing_photos WHERE listing_photo_id=1");
while($row = mysqli_fetch_array($result)) {
$picture=$row['listing_photo_file'];
}
echo $title. "<br/>".$price."<br/>".$picture;
$smarty->assign('naslov', $title);
echo "<br>";
}
}
mysqli_close($con);
echo '<h1>Test</h1>';
}
?>
So I put the script in plugins folder and return TEST echo at the end of script and 3 variables $title, $price, $picture. Using command in template {my_plugin}
But I want to have access to these variables so I can call them in *.tpl file, e.g.: {$title}
That way I can put HTML part in .tpl file and just insert variables I need from that function.
It should loop for 10 results on template file ;)

Theoretically your code have assigned to the naslov smarty variable the last title.
You can change it to append if you would access all of them.
This way theoretically after the {my_plugin} call you can access the titles in the {$naslov} variable.
Or you can return in your template with a $smarty->fetch('othertemplate.tpl') And you can use these variables in othertemplate.tpl.
For limiting the result to at most 10, probably the easiest way to append limit 10 to your db query.

Related

$_GET doesn't work in my program and no lines beneath it are executed

I am trying to add a function to my website where it will add filters to a SQL select statement by allowing the user to enter text in some text boxes and clicking the filter button.
I haven't gotten as far as that because I'm having issues with the $_GET function.
The idea is that if the query parameters are set, it will use them in the sql statement and if not, it will just return all the rows.
Below is the displayListings function which is called every time the page is loaded.
Although I'm showing the whole function, I havent implemented most of it because nothing works past the line where I use $_GET.
<?php
function displayListings() {
global $dbConnection;
//checks if the query parameters exist
if (isset($_GET['title'])) {
echo 'got here... :F';//for debugging
var_dump($_GET['title']);//for debugging
$title_filter = $_GET('title');// THIS IS THE LINE THAT THE SCRIPT STOPS AT
var_dump($title_filter);//for debugging
}
//checks if the query parameters exist
if (isset($_GET['artist'])) {
$artist_filter = $_GET('artist');
echo $artist_filter."/n";
}
//checks if the query parameters exist
if (isset($_GET['release'])) {
$release_filter = $_GET('release');
echo $release_filter."/n";
}
echo 'here!';
// connect to the database
if (!connectToDb('musiconline')) {
$_SESSION['errorMsg'] = "Sorry, we could not connect to the database.";
header('location:listItem.php');
exit();
}
// after this point we have an open DB connection
// gets the current highest ID so we know what the next should be.
$sqlQuery = "SELECT listingid, recordtitle, artist FROM vinyl";
$result = $dbConnection->query($sqlQuery);
if (!$result) {
$_SESSION['errorMsg'] = "There was a problem with the database: " . $dbConnection->error;
closeConnection();
header('location:listItem.php');
exit();
}
//gets the results and puts them in a rows array
while($row = $result->fetch_array()){
$rows[] = $row;
}
//iterates through each row of the results (each vinyl)
foreach($rows as $row){
$listingID = $row['listingid'];
$recordTitle = $row['recordtitle'];
$artist = $row['artist'];
echo '
<div class="listing">
<table class="tableception">
<tr><td><img src="uploads/vinyl'.$listingID.'.png" alt="img1" ></td><td>
<table class="listing-table">
<tr><td>Album title: </td><td>'.$recordTitle.'</td></tr>
<tr><td>Artist name: </td><td>'.$artist.'</td></tr>
</table>
</td></tr>
</table>
</div>
' . "\n";
}//END OF FOREACH
/* free result set */
$result->close();
/* close connection */
closeConnection();
}
?>
When I debug the page in my IDE, everything works fine, probably because there are no query parameters in the URL.
The page stops loading after the $_GET line, as seen below.shows screenshot of browser when query parameters exist in URL.
I just cant figure out what I'm doing wrong.
Thanks in advance.
I used curly brackets instead of square brackets in the line.
I had $title_filter = $_GET('title');
instead of $title_filter = $_GET['title'];
Thanks #AymDev for pointing this out to me!

How to populate multiple items on a different page using a search bar?

Hi I was wondering if anyone could help. I have just developed a search bar for my site which will be on every page loaded using an include function so they will all be the same. The code can read my database perfectly but I am at a lose on how to send more than one result to another page in the format that I need. The problem is that I post 2 to 3 variables to the next page in the url for each link of this sort and if the search bar returns more than 1 result then each result will need 2 to 3 variables to populate the next page.
Link example
Movies Home
Here is the search bar code.
<?php
if(isset($_POST['search_term'])){
$search_term = $_POST['search_term'];
if (!empty($search_term)) {
$query = "SELECT title FROM database WHERE title LIKE '%".mysql_real_escape_string($search_term)."%'";
$query_run = mysql_query($query);
$query_num_rows = mysql_num_rows($query_run);
$result = mysql_query($query_num_rows);
if ($query_num_rows >= 1) {
echo $query_num_rows.' results found:<br>';
while ($query_row = mysql_fetch_assoc($query_run)){
echo $query_row ['title']. '<br>';
}
}
else{
echo 'No results found.';
}
}
}
?>
This code currently echos on the current page. I am hopeing to pass it to a results page and populate multiple items depending on how many results were found in the search bar. Here is an example of the code that I would be hopeing to populate from the search bar on the target page.
<?php
if (isset($var1)){
$subject_set = mysql_query("Select * FROM database WHERE genre like '%".$info."%' and media = '".$med."' ORDER BY ".$sort." ", $connection);}
else{
$subject_set = mysql_query("Select * FROM media", $connection);
}
if (!$subject_set){
die("Database connection failed: " . mysql_error());
}
htlm code</div>
<?php } ?>
I was thinking that if I was even able to pass the results id's using an array and then retrieve the corrosponding results from the database on the target page.
Sorry I am still quite a novice at this type of coding and I hope I did not confuse anyone with what I am trying to say. Thank you for your time and hopefully I can get this problem sorted. Thanks again.
If you want to display the results on other pages, consider putting them in SESSION variables.
<?php
session_start ( ); // at top of page
...
$theIndex=0;
while ($query_row = mysql_fetch_assoc($query_run)){
echo $query_row ['title']. '<br>';
$_SESSION['searchResult'][$theIndex] =$query_row ['title'];
$theIndex++;
}
Then on your the page where you want to display the results, loop through the SESSION['searchResult'] and echo it out...
<?php
session_start ( ); // at top of page
$theResults = $_SESSION['searchResult'];
foreach ($theResults as $key=>$value){
echo htmlentities($value) . "<br>";
}

How do i get a query result as a string into a variable in php?

I have a table having some columns as id ,path, file_type, file_size. Now i need to select the path column for a particular id and obtain the path to a variable as a string. And then using unlink() i want to delete this file using the path stored in the variable.
for that i created two classes but it shows some errors while running it. Any help would apreciated. thanks
first php class
<?php
// Check if delete button active, start this
if(isset($_POST['delete'])&&isset($_POST['checkbox'])){
foreach ($_POST['checkbox'] as $del_id){
$del_id = (int)$del_id;
$res0=$obj->filedelete($del_id);
$res1=$obj->multidelete($del_id);
}
$arr =Array();
$arr=$del_id;
echo $del_id;
if($res1){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=newdelete.php\">";
}
}
?>
Second php class
function filedelete($del_id)
{
echo $q="select path from uploadnw where id='$del_id'";
//echo $q;
$result = $this->includedbcon($q);
echo $result;
return unlink("$result");
}
this is how look like my table
error

php concatenate to display data from another file

So I've got some rows in a table that I want to use to concatenate. I also have a file that runs a query to count comments. I want to pull that number from that file.
I was doing it with straight html but my website is getting too big so I'm trying to make it dynamic with php and mysql.
So with my Html its hard coded and works:
<?php include_once("comments/post_title/tpost_title.php") ?>
So I thought I could just concatenate it to be the same, but for some reason my web browser (chrome) thinks its a comment.
Php:
$query = "SELECT post_title FROM sessions";
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result))
{
echo "<a href=\"#" .strip_tags("{$row['post_title']}"). "\">read comment";
echo "<?php include_once(\"comments/" .strip_tags("{$row['post_title']}")."/t". strip_tags("{$row['post_title']}"). ".php\") ?>";
echo "</a>";
}
I've read about "#file_get_contents" but my understanding of it is limited. As I understand it has to be set to a variable and then I would have to insert that into my while loop. But I get lost in how the variable changes to the next row in my table to pull the next post_title.
I guess another option would be to put the query that's in that file that counts comments into this loop, but then I'd have to put a variable into the query. (Is that "good" coding?) Say for instance:
$query = "SELECT post_title FROM sessions WHERE session = 'variable';
Thanks in advance for the help and insight.
To read the file in the loop, you need:
while($row = mysql_fetch_assoc($result))
{
echo "<a href=\"#" .strip_tags("{$row['post_title']}"). "\">read comment";
include_once("comments/" .strip_tags("{$row['post_title']}")."/t". strip_tags("{$row['post_title']}"). ".php");
echo "</a>";
}
If the file you read isn't PHP but straight HTML, then you can use readfile() instead of include(). The syntax is the same.
To be sure, though, it would be better something like,
$file = "comments/" .strip_tags("{$row['post_title']}")."/t". strip_tags("{$row['post_title']}"). ".php";
if (file_exists($file))
readfile($file);
else
echo "ERROR";

Drupal+Ubercart: Get product list

Does anyone here knows how I could get the list of products including image paths so I could manually process them without using Views?
You can write your own mysql queries or use Drupal's framework to launch your customer queries. Something like:
<?php
$query = "SELECT * FROM uc_products WHERE YOUR_WHERE_CLAUSE_HERE";
$result=db_query($query);
print mysql_result($result, 0);
// you probably want to loop through the $result array
while ($row = mysql_fetch_row($result)) {
echo "Product Title = " . $row['title_field'];
echo "Image Url = " . $row['image_url'];
}
?>
Other tables related to Ubercart that may help:
uc_product_classes
uc_product_features
Put this code in a block or page or wherever you are trying to do it.

Categories