Issues with results from joomla database to screen - database

I seem to be stuck. I have checked this document JDatabase 3.0
And i cant figure out why my rusults from the column wont display on the screen.
Its possible its an issue with the WHERE statement but it does echo out the correct username so I'm not sure. Here is the code I have been manipulating.
{source 0}
<?php
$user =& JFactory::getUser();
if (!$user->guest)
$name = $user->username;
$db =& JFactory::getDbo();
$query = $db->getQuery(true);
$query = "SELECT image_url FROM #__image_data WHERE user_name = $name";
$db->setQuery($query);
$results = $db->image_url(); //also tried $results = $db->loadObjectList
and loadAssocList
echo $results
?>
{/source}

You need
$list = $db->loadObjectList();
foreach ($list as $item){
echo $item->image_url.'<br />';
}

Related

how can i combine href with id number

Trying to get some data from db but I can't use href with id number in code.
I tried everything but coldn't make it.
<?php
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select(array($db->quoteName('title')));
$query->select(array($db->quoteName('id')));
$query->select(array($db->quoteName('start_date')));
$query->select(array($db->quoteName('end_date')));
$query->from($db->quoteName('#__rbid_auctions'));
$db->setQuery($query);
$results = $db->loadObjectList();
// display the results
foreach ( $results as $result) {
echo <<<HTML
$result->title .
HTML;
echo "<p>" . $result->start_date . "</p>";
echo "<p>" . $result->end_date . "</p>";
}
?>
I will be appreciated if someone help me.
Thanks in advance
Demonstration of issue then my suggested solution: (Online Demo)
$result = new stdClass();
$result->id = 333;
$result->title = 'title text';
echo <<<HTML
$result->title .
HTML;
Output:
Notice: Undefined variable: id in /in/s1YZG on line 7
title text .
Notice that $id isn't a declared variable. If it was, it would be rendered but all characters between the curly braces are treated literally because you are trying to echo within an echo.
Without heredoc syntax (heredoc can be funny about tabbing depending on php version):
echo "{$result->title} . "; // curly braces may help IDEs with highlighting
New Output:
title text .
As for your query building syntax...
You can save some typing and chain the method calls onto getQuery().
None of the quoteName() calls are necessary for stability/security, but if you insist on toeing Joomla's preferred practices, you can call quoteName() on the array in select().
Suggested Code:
$db = JFactory::getDbo();
$query = $db->getQuery(true)
->select($db->quoteName(array('title', 'id', 'start_date', 'end_date')))
->from($db->quoteName('#__rbid_auctions'));
$db->setQuery($query);
if (!$results = $db->loadObjectList()) {
echo "No results";
} else {
foreach ($results as $row) {
echo "{$row->title} . ";
echo "<p>{$row->start_date}</p>";
echo "<p>{$row->end_date}</p>";
}
}
Here is another post where loadObjectList() is called after a SELECT query which includes query error checking: https://joomla.stackexchange.com/a/22963/12352
When you have Joomla questions, please post them on Joomla Stack Exchange.
I would try it like this:
also you are trying to echo $id which isnt assigned. should be $results->id
<?php
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select(array('title', 'id', 'start_date', 'end_date' ));
$query->from($db->quoteName('#__rbid_auctions'));
$db->setQuery($query);
$results = $db->loadObjectList();
// display the results
foreach ( $results as $key => $result) {
echo ' <a href="index.php?option=com_rbids&task=viewbids&id='.$result->id .'>'.$result->title .'</a>';
echo "<p>" . $result->start_date . "</p>";
echo "<p>" . $result->end_date . "</p>";
}
?>

store array in database Wordpress

I have this form with a select.
<form>[...]
<select name="quantity[]">
<?php
for ($i=1; $i <20; $i++) {
?>
<option value="<?php echo $i ?>"><?php echo $i ?></option>
<?php } ?>
</select>[...]
</form>
I'm trying to store the quantity data in the database using $wpdb.
global $wpdb;
$table_name=$wpdb->prefix.'prenotazione_eventi';
$wpdb_quantity = maybe_serialize( $_POST[ 'quantity'] );
$query = "INSERT INTO $table_name (quantity) VALUES (%s)";
$prepare_query = $wpdb->prepare( $query, $wpdb_quantity);
$result = $wpdb->query( $prepare_query );
When I submit the form the data is saved in the database, but in this sintax a:1:{i:0;s:2:"10";}. What I want is that the data have only the number that I choose I the form, "10". How do that?
Try this code.
use json_encode instead of maybe_serialize.
global $wpdb;
$table_name=$wpdb->prefix.'prenotazione_eventi';
$wpdb_quantity = json_encode( $_POST[ 'quantity'] );
$query = "INSERT INTO $table_name (quantity) VALUES (%s)";
$prepare_query = $wpdb->prepare( $query, $wpdb_quantity);
$result = $wpdb->query( $prepare_query );

codeigniter value type from db

I have some values in my db (type string: ex id: 0001) but when I fetch data, CI convert it to int and I get 1 as result. How to avoid it? I want to display the db's value 0001 in the front
you can do
Model
function getValues(){
$query = $this->db->get('table_name');
$result = array();
foreach ($query->result() as $row) {
$id = str_pad($row->id, 4, '0', STR_PAD_LEFT);
$result[$id] = $row->type;
}
return $result;
}
Controller
$result = $this->model_var->getValues();
$data['result'] = $result;
$this->load->view('view_name',$data);
View
foreach($result as $key => $val) {
echo 'key: ' . $key . ', value: ' . $val;
}

Communicating with a DB in LimeJS

What is the proper way to load variables into my game from a DB?
I tried using Ajax and the Prototype library, but that doesn't seem to work. Here's what I did:
In my main .js game file...
var rawVocab = new Array();
var optionVocab = new Array();
new Ajax.Request('load_vocab.php', {
onSuccess : function(xmlhttp) {
eval(xmlhttp.responseText);
}
});
'load_vocab.php' looks like this...
<?php
header('Content-Type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>';
echo '<response>';
$username = "user";
$password = "***************";
try {
$conn = new PDO('mysql:host=localhost;dbname=tygrif_school', $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare('SELECT * FROM vocabulary_game WHERE game_type = :game_type');
$stmt->execute(array('game_type' => 'target'));
$i=0;
while($row = $stmt->fetch()) {
echo "rawVocab[".$i."]['word']='".$row['word']."';";
echo "rawVocab[".$i."]['translation']='".$row['translation']."';";
echo "rawVocab[".$i."]['example_sentence_1']='".$row['example_sentence_1']."';";
$i++;
}
$stmt = $conn->prepare('SELECT * FROM vocabulary_game');
$stmt->execute(array());
$i=0;
while($row = $stmt->fetch()) {
echo "optionVocab[".$i."]['word']='".$row['word']."';";
echo "optionVocab[".$i."]['translation']='".$row['translation']."';";
echo "optionVocab[".$i."]['example_sentence_1']='".$row['example_sentence_1']."';";
$i++;
}
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
echo '</response>';
?>
Is there some built in way to handle this with the goog library?
Apparently, Google Closure has a built-in way (goog.net.XhrIo) to handle Ajax calls.
1 - http://docs.closure-library.googlecode.com/git/class_goog_net_XhrIo.html
2 - http://www.daveoncode.com/2009/11/17/goog-net-xhrio-make-simple-ajax-calls-with-google-closure/

Drupal 7 Printing two records with same name

Does anyone know how to target two specific records with the same name seperately?
ie. I'm trying to print two fields from the database with the same nameā€¦ of course it gives me the same result.
Slightly abbreviated code:
$query = db_select('node', 'n');
$query->join('users', 'u', 'u.uid = n.uid');
$query->join('taxonomy_term_data', 'td', 'td.tid = ti.tid');
$query
->fields('u', array('name'))
->fields('td', array('name'))
foreach ($result as $record) {
User Name: <?php echo $record->name; ?>
Tag Name : <?php echo $record->name; ?>
}
I can't find the answer but guessing something like this
<?php echo $record->td['name']; ?>
But no luck.
You can use the addField method to add aliases to your fields. Your query can be rewrited like this :
$query = db_select('node', 'n');
$query->join('users', 'u', 'u.uid = n.uid');
$query->join('taxonomy_term_data', 'td', 'td.tid = ti.tid');
$query
->addField('u', 'name', 'username')
->addField('td', 'name', 'tagname')
foreach ($result as $record) {
User Name: <?php echo $record->username; ?>
Tag Name : <?php echo $record->tagname; ?>
}

Resources