how can i combine href with id number - database

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>";
}
?>

Related

Custom Fields - Repeater dropdown - list only unique & not blank values

Here's the thing that I'm using the ACF repeater subfield (firma) to store some data. Usually the user is typing here his parent company name like: google, alibaba, sodexo etc. So it might happen that in multiple posts, the value of this field will be the same. At the moment I have following code:
$args = array(
'post_type' => 'opencourses',
'meta_key' => 'terminy_warsztatow'
);
$the_query = new WP_Query($args);
if ($the_query->have_posts()):
echo '<select type="text" class="form-control" name="filtr_lokalizacja">';
while ($the_query->have_posts()) : $the_query->the_post();
if(have_rows('terminy_warsztatow')):
while (have_rows('terminy_warsztatow')) : the_row();
// display your sub fields
$filtr_var = get_sub_field('firma');
echo '<option value="'. $filtr_var .'">';
echo $filtr_var;
echo '</option>';
endwhile;
else :
// no rows found
endif;
endwhile;
echo '</select>';
endif;
And it works - meaning: it shows all typed values. But instead of showing only UNIQUE values it generates a list similar to this:
Google
Alibaba
Google
Sodexo
Sodexo
Tesla
Tesla
Sodexo
How to avoid showing same values and hide empty as well? I know that there is php function array_unique but I was unable to implement that. I've done sth like:
$filtr_var = get_sub_field('firma');
$result = array_unique($filtr_var);
echo $result;
but then it shows no values at all.
I assume that "firma" is simple text input type in repeater. if so then arrya_unique function won't work for string output.
you need to save each value in array and then use in_array function to make it unique.
see below code.
$args = array(
'post_type' => 'opencourses',
'meta_key' => 'terminy_warsztatow'
);
$the_query = new WP_Query($args);
if ($the_query->have_posts()):
echo '<select type="text" class="form-control" name="filtr_lokalizacja">';
while ($the_query->have_posts()) : $the_query->the_post();
if(have_rows('terminy_warsztatow')):
// $PreValue = array();
while (have_rows('terminy_warsztatow')) : the_row();
// display your sub fields
$filtr_var = get_sub_field('firma');
// compare current value in saved array
if( !in_array( $filtr_var, $PreValue ) )
{
echo '<option value="'. $filtr_var .'">';
echo $filtr_var;
echo '</option>';
}
// save value in array
$PreValue[] = $filtr_var;
endwhile;
else :
// no rows found
endif;
endwhile;
echo '</select>';
endif;
Hope this will help you!
Enjoy

Codeigniter form populate with db results not working

I have been trying for days now using the form class to populate the text fields from my database. I have also been searching to find out how to do it without any luck.
Please could somebody take a look at my code and tell me what I'm doing wrong.
Model
//This function brings up the selected users information for editing.
public function edit_user($id)
{
$this->db->select('id, email, name, lastname, homeaddress, posteladdress,
mobile, hometel, idnum');
$this->db->where('id', $id)->limit(1);
$query = $this->db->get('user');
return $query->result();
}
Controller
public function get_user_edit($id)
{
$this->load->helper('form');
$this->load->model('model_users'); //Load the user model
//Get the database results from the model
$data['results'] = $this->model_users->edit_user($id);
foreach ($data['results'] as $key => $row)
{
$data['results'] = array( 'id' => $row->id,
'email' => $row->email,
'name' => $row->name,
'lastname' => $row->lastname,
'homeaddress' => $row->homeaddress,
'posteladdress' => $row->posteladdress,
'mobile' => $row->mobile,
'hometel' => $row->hometel,
'idnum' => $row->idnum);
}
$this->load->view('edit_user', $data);
}
View
<div id="body">
<p>Edit user information.</p>
<?php
echo form_open('user_admin/user_update', $results);
echo validation_errors();
echo "<p><lable>Email:</lable>";
echo form_input('email', set_value('email'));
echo "</p>";
echo "<p><lable>Name:</lable>";
echo form_input('name', set_value('name'));
echo "</p>";
echo "<p>";
echo form_submit('edit_submit', 'Update');
echo "</p>";
echo form_close();
?>
I keep getting this error
A PHP Error was encountered
Severity: 4096
Message: Object of class stdClass could not be converted to string
Filename: helpers/form_helper.php
Line Number: 1010
First thing. You're expecting a single row to be returned from the database, right? But you're looping through the result in your controller. Instead of doing that in your model:
return $query->result();
do this:
return $query->row();
and remove the foreach loop from your controller (it even has an unused $key var in there). You'll end up having cleaner and shorter code. Only if you're getting a single row, which seems to be the case.
Moving on.
Have you read the documentation here: https://ellislab.com/codeigniter/user-guide/helpers/form_helper.html ?
form_open('user_admin/user_update', $results); - what are you trying to do here?
You use the second parameter to pass attributes to the opening tag. Now looking at your code, your $results array has values for each field. Doesn't make much sense to push all this into the form opening tag, does it?
Field rendering with the correct values.
This is an example from the documentation how to configure and render an input field:
$data = array(
'name' => 'username',
'id' => 'username',
'value' => 'johndoe',
'maxlength' => '100',
'size' => '50',
'style' => 'width:50%',
);
echo form_input($data);
So, your email input should be formatted having the minimal config like that:
$data = array(
'name' => 'email',
'value' => $results['email']
);
echo form_input('email', $data);
On validation (in your user_update() method in user_admin controller), you'll have to do some logic to capture the value you're validating. So you'll need something like:
$results['email'] = set_value('email');
Hope that makes sense!
Thank you very much for your help. I'm sure you must have realised that I'm new to Codeigniter, but with your help I was able to sort out the problem that I had.
I changed the query to return only the one result that it found like you suggested
return $query->row();
and you were right I didn't need the foreach loop to populate an array to pass to the view.
Thanks for pointing out that I was trying to pass the results from db as the attribute for the form.:)
I added my code below for anybody that come across my question with the same problem. Maybe it will help them.
My controller
public function get_user_edit($id)
{
$this->load->model('model_users'); //Load the user model
//Get the database results from the model
$data['results'] = $this->model_users->edit_user($id);
$this->load->view('edit_user', $data);
}
My model
//This fetches the selected users information for editing.
public function edit_user($id)
{
$this->db->select('id, email, name, lastname, homeaddress, posteladdress,
mobile, hometel, idnum');
$this->db->where('id', $id)->limit(1);
$query = $this->db->get('user');
return $query->row();
}
My view
<div id="container">
<h1>Edit user</h1>
<div id="body">
<p>Edit user information.</p>
<?php
echo form_open('user_admin/user_update');
echo validation_errors();
echo form_hidden('id', $results->id);
echo "<p><lable>Email:</lable>";
echo form_input('email', $results->email);
echo "</p>";
echo "<p><lable>Name:</lable>";
echo form_input('name', $results->name);
echo "</p>";
echo "<p><lable>Last name:</lable>";
echo form_input('lastname', $results->lastname);
echo "</p>";
echo "<p><lable>Home address:</lable>";
echo form_input('homeaddress', $results->homeaddress);
echo "</p>";
echo "<p><lable>Postal address:</lable>";
echo form_input('posteladdress', $results->posteladdress);
echo "</p>";
echo "<p><lable>Mobile number:</lable>";
echo form_input('mobile', $results->mobile);
echo "</p>";
echo "<p><lable>Home telephone:</lable>";
echo form_input('hometel', $results->hometel);
echo "</p>";
echo "<p><lable>ID number:</lable>";
echo form_input('idnum', $results->idnum);
echo "</p>";
echo "<p>";
echo form_submit('edit_submit', 'Update');
echo "</p>";
echo form_close();
?>
</div>

WP_Query echoing arrays in different spots

Hello I need to echo a WP_Query in different areas so I can add content between each render post title..
SO basically I want to manual breakout the posts so I can place them around static content. Hope this is making sense.
I want to do something like this. Please see my notes.
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ) :
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>'; // first post title
//echo 'some static content';
echo '<li>' . get_the_title() . '</li>'; //second post title
//echo 'some more static content';
endwhile;
You can use an array with all the static content:
$the_query = new WP_Query( $args );
$static_content = array ('content after first post','content after second post');
$counter = 0;
while ( $the_query->have_posts() ) :
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
echo $static_content[$counter];
$counter++;
endwhile;

Issues with results from joomla database to screen

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 />';
}

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