I have the TPLancer component for Joomla installed on my site and I am trying to add some more information to the profile page of both the Freelancer and the Buyer. I am no genius with PHP or mySQL but can kind of bluff my way thru usually.
The information currently displayed on the profile pages is just data pulled from the database. I have added a new ‘website’ row in the database and have entered some data into the database but I can’t seem to get it to display. I thought it would just be something like echo $row->website; because echo $row->categories; works and so does echo $row->company; and they are all in the same table.
There is something happening that is obviously way above my head. Below I have pasted a section of the code from the tplancer.php file which contains the function for what I am trying to target.
Below that is some code from the tplancer.html.php file which displays the actual HTML on the page.
I hope someone can help and I hope I don’t sound too dumb! Cheers
function showLancerInfo($option)
{
global $mainframe;
$user =& JFactory::getUser();
$db =& JFactory::getDBO();
$lancer_id = JRequest::getVar('id', 0, 'get', 'int');
$is_lancer = isLancer($user->id);
$is_buyer = isBuyer($user->id);
$query = "select id FROM #__glance_lancer where userid =".$lancer_id;
$db->setQuery( $query );
$id = $db->loadResult();
if(!$id)
{
echo "Freelancer not found!";
return;
}
$query = "select username FROM #__users where id =".$lancer_id;
$db->setQuery( $query );
$username = $db->loadResult();
$row =& JTable::getInstance('lancer','Table');
$row->load($id);
$query = "select * FROM #__glance_projects where chosen =".$lancer_id." ORDER BY id desc LIMIT 10";
$db->setQuery( $query );
$projects = $db->loadObjectList();
$query = "select * FROM #__glance_bids where userid=".$lancer_id." order by id desc LIMIT 10 ";
$db->setQuery( $query );
$bids = $db->loadObjectList();
HTML_front_glance::showLancerInfo($option,$row,$projects,$bids,$username);
}
function showLancerInfo($option,$row,$projects,$bids,$username)
{
global $mainframe, $option;
$user =& JFactory::getUser();
$lancer_info = getUserInfo($row->userid);
?>
<!-- /////////////////////// FREELANCER PROFILE PAGE /////////////////////// -->
<?php
$img = JPATH_SITE.DS.'images'.DS.'glance'.DS.$row->userid.'.jpg';
if(file_exists($img)) {
$img = JURI::base().'images/glance/'.$row->userid.'.jpg';
echo '<img class="profile_logo" src="'.$img.'" alt="Logo" />';
}
else
{
$img = JURI::base().'components/com_glance/images/noimage.jpg';
echo '<img class="profile_logo" src="'.$img.'" alt="Logo"/>';
}
?>
<div class="profile_info">
<h1><?php echo $lancer_info['username'] ?><?php if($row->special=='y') {?>
<img src="components/com_glance/images/featured.png" alt="Featured" class="feat_lancer" />
<?php } ?></h1>
<h3><?php echo $row->company ;?></h3>
<p><?php echo $row->categories ; ?></p>
<p>Website: <?php echo $row->website; ?></p>
<p>Hourly Rate US $<?php echo $row->rate; ?></p></code>
Trying look for a folder in the component called "tables". Find the correct file, it should be called "lancer", in there that defines the table variables for the table you want and add the new variables you wish to display - e.g.
...
var $website = null;
...
Then you will be able to use, as you said, $row->website.
The clue you are looking for in the model function is this line:
$row =& JTable::getInstance('lancer','Table');
I hope that solves your problem! Good luck!
Related
I have a table in the database which contains 2 columns one for PLZ (zip code) and the other for Link
and I have a form that contains an input and a button.
the work required is when I type the PLZ in the form and I click on the button we will give the link corresponding to this PLZ
<?php
require('../../../wp-blog-header.php');
require('../../../wp-config.php');
if(isset($_POST['submit']))
{
// WP Globals
global $table_prefix, $wpdb;
// Customer Table
$customerTable = $table_prefix . 'customer';
$PLZ = $_POST['PLZ'];
// search in all table columns
$query = "SELECT Link
FROM $customerTable
WHERE PLZ = '$PLZ'
";
$search_result = submit($query);
}
else {
echo 'error';
}
// function to connect and execute the query
function submit($query)
{
global $wpdb ;
$search_result = $wpdb->get_results($query);
foreach($search_result as $row){
header('Location: '.$row['Link']);
}
}
?>
and this is the form
<?php
function oped_postcode_form_function() {
<form method="get" action="<?php echo plugins_url('action.php', __FILE__ ); ?>">
<label>Postleitzahl</label><input type="text" pattern="[0-9]{5}" title="Five digit zip code" />
<button name="submit">submit</button>
</form>
<?php
}
// register shortcode
add_shortcode('oped_postcode_form', 'oped_postcode_form_function');
?>
the result always gives error
Your form send GET request to server, so you need to use $_GET array in PHP code:
<?php
require('../../../wp-blog-header.php');
require('../../../wp-config.php');
if(isset($_GET['submit']))
{
// WP Globals
global $table_prefix, $wpdb;
// Customer Table
$customerTable = $table_prefix . 'customer';
$PLZ = $_GET['PLZ'];
// search in all table columns
$query = $wpdb->prepare("SELECT Link FROM $customerTable WHERE PLZ = %s", $PLZ);
$search_result = submit($query);
}
else {
echo 'error';
}
// function to connect and execute the query
function submit($query)
{
global $wpdb ;
$search_result = $wpdb->get_results($query);
foreach($search_result as $row){
header('Location: '.$row['Link']);
}
}
?>
Also you should to use prepared statements to prevent SQL Injection
I am trying to fill the article title automatically into an event creation form inside a EasySocial stream module. Therefor I need to insert the current article title as title variable inside the .php file that creates the event.
If I just try
$title = TITLE;
it works and the event gets the title "TITLE". But if I try to catch the current article title first and output the article title as title variable, it does not work. Obviously I am doing something wrong but I can not discover how to get it working. I got this so far:
$articleTitle = '';
$input = JFactory::getApplication()->input;
if ( ($id = (int) $input->get('id')) )
{
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('title')->from('#__content')->where('id = ' . $id);
$db->setQuery($query);
$articleTitle = $db->loadResult();
}
echo $articleTitle;
This is working and I can see the article title on my site. But when I try to set it as $title to be set, it is not working any longer:
$title = $articleTitle;
Any advice what I am doing wrong at the final step?
Your articleTitle is not string but db result
if (!$result) {
echo 'No title ' . mysql_error();
exit;
}
$row = mysql_fetch_row($result);
echo $row[0]; // title
$doc = JFactory::getDocument();
$title = $doc->getTitle();
$title equals title of the page
Don't forget echo before $title
Hello I am just learning PHP and have decided to create a blog. I have figured out how to create a database and add entries to it. but am having trouble figuring out how to display those entries in an organized manner.
this is where I have gotten so far:
<?php
mysql_connect ('localhost', 'user', 'password') ;
mysql_select_db ('db');
$sql = "SELECT * FROM tbp_blog ORDER BY timestamp DESC LIMIT 5";
$result = mysql_query($sql) or print ("Can't select entries from table tbp_blog.<br />" . $sql . "<br />" . mysql_error());
while($row = mysql_fetch_array($result)) {
$date = date("l F d Y g:i:s A", $row['timestamp']);
$link = $row['link'];
$title = stripslashes($row['title']);
$description = stripslashes($row['description']);
$entry = stripslashes($row['entry']);
$image_link = $row['image_link'];
$image_alt = $row['image_alt'];
?>
<ul class="submissions">
<li class="first">
<a href="<?php echo $link; ?>">
<img alt="<?php echo $image_alt; ?>" class="blog_image" height="198" src="<?php echo $image_link; ?>" title="<?php echo $title; ?>" width="276" /></a>
<div class="submissions_content">
<h3><?php echo $title; ?></h3>
<p><?php echo $description; ?></p>
</div>
</li></ul>
<div class="hr">
<img class="star" src="images/star.png" width="40" height="12" alt="Star" /></div>
<?php
}
?>
this is a link to a page that is an example of exactly what I am trying to get:
http://wearepandr.com/blog
how can I get my entries to display in descending order, in rows of three, with a limit of 9 entries per page. As well as automatically creating the next page and so on. I see that they have used li classes first second and last for the order of each row.
I am also wanting to format my time stamp so that it indicates to time in my timezone Vancouver BC.
Any help would be greatly appreciated. Thanks in advance.
Mario
Here is an example in PDO
First, connecting to the database:
$server = 'localhost';
$database = 'db_name';
$db_username = 'username';
$db_password = 'password';
$dsn = "mysql:host=$server;dbname=$database";
try {
$db = new PDO($dsn, $db_username, $db_password);
}
catch (PDOException $e) {
//You can do what you like with any error messages (echo, store, etc)
$error_message = $e->getMessage();
}
Next, creating the function that will have the query in it:
Query Functions go in the Model (MVC)
//Use a function that can accept parameters (users id, search term, etc)
//For your question, there are no parameters
function getPosts() {
global $db;
global $database;
$sql = "SELECT * FROM $database.tbp_blog ORDER BY timestamp DESC LIMIT 5";
//If your query was more complex, you would need to learn about binding values
try {
$stmt = $db->prepare($sql);
$stmt->execute();
$posts = array();
//Depending on how many posts you have, you can do fetch or fetchAll
//fetchAll is faster to write but here is the approach with fetch
$result = $stmt->fetch();
while ($result != NULL){
$posts[] = $result;
$result = $stmt->fetch();
}
//Free up resources
$stmt->closeCursor();
//return it to where it was called from
//send back the array we created
return $posts;
}
//Catch any errors
catch (PDOException $exc) {
return '0';
}
}
Call the function
Usually this goes in the controller (MVC)
//Call the function to get posts with no parameters
//The variable "posts" here will become the returned "posts" array from our function
$posts = getPosts();
This is in the HTML section of your site:
This is in the view (MVC)
<?php
if (empty($posts)) {
echo 'There are no results';
}
else {
echo '<ul class="submissions">';
//You can decide what type of loop to use
foreach ($posts as $post) { ?>
<li class="first"> <img alt="<?php echo $post['image_alt']; ?>" class="blog_image" height="198" src="<?php echo $post['image_link']; ?>" title="<?php echo $post['title']; ?>" width="276" />
<div class="submissions_content">
<h3><?php echo $post['title']; ?></h3>
<p><?php echo $post['description']; ?></p>
</div>
</li>
<?php }
echo '</ul>';
}
?>
This code looks like much more than it is because I added in a fair amount of comments for you to read while implementing it in your site.
If you are going to be creating a blog, it would be very wise to learn about the Model View Controller (MVC) for organizing and using your code. Most blogs are built that way.
i have two types of users on my site, 'free' and 'premium'.
basically i have a messaging system that lists the message and the user who send the message.
when you click on the user's name/image it will link to 'profile.php?id=(user_id)'
what i am trying to do, is if the account type of the user who sent the message = 'free' then i want to take the user to a different link when clicked on.
i am really new to php and am not sure what to do, please can someone show me an example of how i can do this. here's my current code.
function:
function message_account_type() {
global $connection;
global $_SESSION;
global $profile_id;
global $message_id;
$query = "SELECT ptb_users.account_type, ptb_messages.from_user_id
FROM ptb_users, ptb_messages
WHERE ptb_messages.from_user_id = \"$profile_id\"
AND ptb_profiles.user_id = ptb_messages.from_user_id ";
$message_account_type = mysql_query($query, $connection);
confirm_query($query, $connection);
return $message_account_type;
}
php:
<?php
$message_account_type = message_account_type();
while ($type = mysql_fetch_array($message_account_type))
if ($type['account_type'] == 'Premium') {
echo "<?php echo "<a href=\"profile.php?id={$inbox['from_user_id']}\">{$inbox['display_name']}"; ?><? } ?>
<?php
$message_account_type = message_account_type();
while ($type = mysql_fetch_array($message_account_type))
if ($type['account_type'] == 'Free') {
echo "<?php echo "<a href=\"members.php?id={$inbox['from_user_id']}\">{$inbox['display_name']}"; ?><? } ?>
Your best bet is to serve up the relevant template file inside of profile.php. For example, on your profile.php you could do something like this:
$the_user = new User($_REQUEST['id']);
$include_file = ($the_user->account_type == 'free') ? 'free' : 'premium';
include($include_file.'-profile.php');
I seriously have NO IDEA why so many of you open and close <?php tags within a document for no reason...
well a shorter version would be:
<?php
function is_premium_user($profile_id)
{
$query = sprintf("SELECT ptb_users.account_type WHERE ptb_messages.from_user_id = '%s' AND account_type = 'Premium' LIMIT 1", $profile_id );
$result = mysql_query($query);
if (!mysql_fetch_assoc($result))
return false;
return true;
}
$message_account_type = message_account_type();
while ($type = mysql_fetch_array($message_account_type))
if (is_premium_user($profile_id))
echo ' <a href="profile.php?id='.$inbox['from_user_id'].'">'.$inbox['display_name'].'';
else
echo ' <a href="members.php?id='.$inbox['from_user_id'].'">'.$inbox['display_name'].'';
?>
How could I have my database connection information at the top of my page but have the query separate from the code but still related so that I could have the print or echo displaying the retrieved images in a Div that I could place anywhere on my site?
<?php
$db = new PDO('mysql:host=host;dbname=dbname;charset=UTF-8', 'username', 'password');
$q = "SELECT badge1 FROM user_badges WHERE username='{$_SESSION['username']}' LIMIT 1";
$r = mysql_query($q);
$row = mysql_fetch_assoc($r);
$badge1 = $row['badge1'];
if($badge1 == "unlocked") {
print "image if user has unlocked the badge";
} else {
print "image if user hasn't unlocked the badge";
}
?>
I would like to have the above if and else statements placed in a div so I could position the images printed from the query anywhere on my site. Is this possible?
A couple of ways. Here is one example:
<?php
$db = new PDO('mysql:host=host;dbname=dbname;charset=UTF-8', 'username', 'password');
$q = "SELECT badge1 FROM user_badges WHERE username='{$_SESSION['username']}' LIMIT 1";
$r = mysql_query($q);
$row = mysql_fetch_assoc($r);
$badge1 = $row['badge1'];
?>
//Break out of PHP and add any HTML elements you want
<div id="whatever">
<?php
if($badge1 == "unlocked") {
print "image if user has unlocked the badge";
} else {
print "image if user hasn't unlocked the badge";
}
?>
</div>
p.s. Check your PDO. It's mixed with old MySQL_ stuff