I am trying to post pictures in a joomla page. I am completely new in php and joomla.
This is what I have in a joomla page:
<h1>Welcome to my home page!</h1>
<p>Some text.</p>
<p>Some more text.</p>
<p><img src="images/test.php?id=8" alt="powered by" />
</p>
<p>Done!</p>
this is test.php:
<?php
$link=...... //connection to database
if(isset($_GET['id']))
{
$id = mysql_real_escape_string($_GET['id']);
$query=...
$result=mysqli_query($link,$query);
while($row=mysqli_fetch_array($result)) {
$imageData = $row['photo'];
}
header("content-type: image/png");
echo $imageData;
}
?>
This is just for a simple 1 photo print, I eventually want to print multiple photos, and have like a slideshow.
Is this possible using joomla?
I also tried several other ways, but I had no luck. Is it possible to have ?
I just need a way through loop through images that are in a database, and print them out. Is there a good approach to this than what I am taking? Do I have to install plugins?
This is a completely wrong approach.
You have to develop a custom module to handle your slideshow task. You could start here.
As for your current code you have to convert it to:
<?php
$db = JFactory::getDbo();
$query = $db->getQuery(true)
->select($db->quoteName('id'))
->from($db->quoteName('#__helloworld'))
->where('id = '. $db->Quote($params));
$db->setQuery($query);
$results = $db->loadObjectList();
print_r($results);
?>
Please check the full list of joomla database functions.
This part will help you how you could display your results.
Good Luck!
Related
In PHP i'm using the following code to put a banner at the top of my website.
$eventinfo = simplexml_load_file("eventinfo.xml");
<div id="eventinfo"><?php foreach($eventinfo->children() as $child){ $final = $child["name"]."...<a href='".$child["adr"]."'>more info...</a>"; } ?>
</div>
The XML doc is available at the following: http://eastsidespeedway.raceresults.co/eventinfo.xml
If you go to http://eastsidespeedway.raceresults.co/index.php you'll see that the more info... link is showing up twice. One with the correct link, and the other with a link to the same page (index.php).
Can anyone shed some light on what I'm doing wrong?
Also. If you see anything that I'm doing wrong, or you know something that's easier - let me know! This is my first time using XML/PHP so I'm kinda just wingin it. Haha.
this will work for you
<?php
$doc = new DOMDocument();
$doc->load('http://eastsidespeedway.raceresults.co/eventinfo.xml');
$title = $doc->getElementsByTagName('title');
$link = $doc->getElementsByTagName('link');
//print_r($eventinfo);
?>
<div id="eventinfo">
<?php echo $title->item(0)->getAttribute('name'); ?>
<a href='<?php echo $link->item(0)->getAttribute('adr'); ?>'>More Infoo..</a>
</div>
If you look at your source:
<div id="eventinfo">5/18/2013 - Al Smiley Memorial...<a href=''>more
info...</a>...<a href='http://www.eastsidespeedway.com/dirt.html'>more
info...</a></div>
You've got two hyperlinks- one href is blank meaning that it will redirect to the current page, check your HTML code first to see if you've accidentally duplicated the element, otherwise look at the construction of your string in the php code
I have a the following code that i intend to should return a filepath name from mysql in the php section and then display the image in html. But i am only getting up a tiny thumbnail in my browser. By the way "username" and "imagefile" are the only columns in the table "images".
I'm sure there is just some silly mistake but i need a fresh pair of eyes to spot it. Thanks a lot in advance. P.S i know i should really me moving over to mysqli but i will simply translate at a later date. Cheers
<?php
session_start();
$username = $_SESSION['username'];
$con = mysql_connect('localhost','root','password');
mysql_select_db("db");
$profileimage = mysql_query("
SELECT * FROM images WHERE username='$username'
");
$row = mysql_fetch_array($profileimage);
$showimage = $row['imagefile'];
?>
<html>
<img src = "$showimage">
</html>
First off, HTML doesn't know what "$showimage" means. That is a PHP variable and HTML cannot interpret it. You need to output it so that HTML can just deal with the result.
So if the value for $showimage is "/images/foo.jpg" you would need something like:
<img src="<?php echo $showimage; ?>" />
which would give you
<img src="/images/foo.jpg" />
Now, switching things to mysqli is as simple as replacing mysql with mysqli. It's no more complicated than that. Since it looks like you are just starting to learn about these things you may as well, when you go to improve things, learn about PDO.
Is this your current real code or is it a simplified version? If it is your real code the problem is in the HTML part where the PHP variable is unknown, you should do this:
<html>
<img src ="<?php echo $showimage; ?>" />
</html>
<?php
$db = mysqli_connect("localhost:3306","root","","databasename");
$sql = "SELECT * FROM table_name ";
$sth = $db->query($sql);
while($result=mysqli_fetch_array($sth)){
echo '<img src="data:image/jpeg;base64,'.base64_encode( $result['image'] ).'" height="100" width="100"/>';
}
?>
Works for me
I am storing in a mySQL table the HTML/PHP content of individual slides to be displayed on a single page.
Here is an example of HTML/PHP code stored in the mySQL table:
<p>Welcome <?php echo $userData['fname']; ?>!</p>
<p>You made it to the first slide!</p>
I retrieve the content of the slides in PHP with the following code:
<?php
$fetchedPageSlideData = mysql_query("SELECT * FROM pageSlides WHERE pageID = $pageID ORDER BY 'order' DESC") or die(mysql_error());
while ($pageSlideData = mysql_fetch_array($fetchedPageSlideData)) {
$pageSlideContent = $pageSlideData['content']; ?>
<div><?php echo $pageSlideContent; ?></div>
<?php }
?>
All of the HTML of the content displays correctly, but the PHP is inserted as follows:
<!--?php echo $userData['fname']; ?-->
So the PHP is commented out and doesn't display.
How can I retrieve the HTML/PHP code and have the PHP not commented out?
It might be a better idea to use placeholder strings in the DB data. Executing arbitrary php code from a DB can be dangerous. PHP is Evil
Look into PHP function eval(): http://php.net/manual/en/function.eval.php
Dropping in and out of the PHP interpreter makes your code rather difficult to read. Consider:
<?php
$f = mysql_query(
"SELECT *
FROM pageSlides
WHERE pageID = $pageID
ORDER BY 'order' DESC"
) or die(mysql_error());
while ($d = mysql_fetch_array($f)) {
print "<div>" . $d['content'] . "</div>\n";
}
Regardless there is no implicit nor explicit mechanism here which would inject the comment tags you've presented. However it may be the browser trying to make sense of the unescaped html code and <?php ... ?> tags.
Try:
print "<div>" . htmlentities($d['content']) . "</div>\n";
As a side note, you might consider using
print "<div>" . highlight_string($d['content']) . "</div>\n";
Or do you mean that you actually want to run the code stored in the database - if so, you're asking for a world of pain. Eval is not evil - but you really must know what you're doing to avoid getting bitten by it.
I have a WordPress site for a client. He owns a video store, and I made a site for him to update the list of movies, usually just the "new this week" movies.
I used PodCMS as an easy way for him to upload movies and then display them. No need for him to even create posts. Works really well, it's a great extension, I'm just having some issues.
The Pod has a field where you insert the release date. 2010-04-20
Then there is a Pod page/template combo that calls the movies with a certain release date like this:
$date = pods_url_variable('last');
He then just creates a blank WP page with the slug 2010-04-20
So when you open that page, the Pod page/template reads that slug and renders a list of the appropriate movies.
My problem:
I need these to be searchable. Is this possible.
I'm also open to suggestions on other ways to go about making this site work. I need it to be as simple as that. Uploads some movies and creates a new page. Then the rest is done automatically.
A PodsCMS search is nothing more than a mySQL table search for the search term. You can search the title, body, pretty much anything. Here's an example:
Note: I'm using "whatever" as the pod info. I'm also forming a string that goes into the $where position which comprises the various pods variables I want to search on. Also, I'm assuming pagination using the Pods pagination controls but I want that variable carried across the pages so I can offset.
<?php
$search_term = $_GET["s"];
$paged = get_query_var('paged');
$page_number = $_GET['pg'];
?>
<h1>
Results for "<?php echo $search_term; ?>"<?php if($page_number > 1){ ?> (Continued)<?php } ?><?php if($paged > 1){ ?> (Continued)<?php } ?>
</h1>
<?php if($paged <= 1){ ?>
<h2>Results in Whatever...</h2>
<?php
$whateverSentence = "(t.name LIKE '%" .$search_term. "%') || (t.whatever LIKE '%" .$search_term. "%')";
$whatever = new Pod('whatever');
$whatever->findRecords($orderby = 't.whatever DESC', $rows_per_page = 5, $where = $whateverSentence, $sql = null);
$total_whatever = $whatever->getTotalRows();
?>
<?php if( $total_whatever >0 ) : ?>
<?php while ( $whatever->fetchRecord() ) : ?>
<?php
// Set Variables
$whatever_ID = $whatever->get_field('id');
$whatever_Permalink = $whatever->get_field('slug');
$whatever_Name = $whatever->get_field('name');
?>
Code that echos the pods variables and represents the search result
<?php endwhile ?>
<?php else: ?>
<p>Sorry, no results found for that search.</p>
<?php endelse; endif ?>
<?php echo $whatever->getPagination($label = '<span class="pagination-text">Go to page:</span>'); ?>
<?php } ?>
So you need the WordPress page content - the list of movies - to be searchable?
WP doesn't search pages natively, but will with WordPress › Search Everything « WordPress Plugins and search better with WordPress › Relevanssi « WordPress Plugins
header.php
<?php
$conn = mysql_connect('localhost', '-', '-');
#mysql_select_db('accmaker', $conn) or die("Unable to select database");
?>
<html>
<head>
<title>Mysite.com - <?php isset($pageTitle) ? $pageTitle : 'Home'; ?></title>
</head>
<body>
profile.php
require 'header.php';
$q = mysql_query("SELECT * FROM users WHERE username = '$username'");
$r = mysql_fetch_assoc($q);
$pageTitle = "Profile of $r[username]";
I think you understand what i want
I cant include header.php after the query, because i wont be connected to mysql
waht do you suggest other than having the connection snippet on every page
What do I suggest? A MVC (Model-View-Controller) Framework like Kohana. If you don't want to go that route, break your connection off into its own file:
<?php
# connect
require_once("connection.php");
# load page data array
require_once("page-data.php");
?>
...
<title><?php print $page["title"]; ?></title>
Note here how I have a $page array of data. This will be helpful when debugging later rather than having several independent variables. With an array of page data, I can quickly see all of the information laid out for any given page:
print "<pre>";
print_r($page);
print "</pre>";
Determining your title should be done within page-data.php, rather than on your page:
$config["site_name"] = "Bob's Shoe Mart";
$config["admin_email"] = "bob#shoemart.com";
/* query to get $row['title'] */
$page["title"] = (!empty($row["title"])) ? $row["title"] : $config["site_name"] ;
Not sure of a "best" solution, but we currently include multiple files. We have our "utilities.php" file that connects to the database and provides some nice functions. We then set our page titles and then we include "top.php" which is the layout portion. It doesn't have anything except HTML with a little bit of PHP for display purposes. Looks like this:
include "utilities.php";
$pageTitle = "Welcome";
include "top.php";