I've created a form using the component SEBLOD for joomla, it's all right, I can properly view the data entered, but i would like to manipulate the data, however I am not able to connect to the database, so I tried this:
<?php
defined( '_JEXEC' ) or die;
$catId = 11;
$catId2 = 12;
$sql = "SELECT * FROM #__cck_store_form_add_compras";
$con = &JFactory::getDBO();
$con->setQuery($sql);
$resultado = $con->loadObjectList();
$query = "SELECT * FROM #__content WHERE catid ='" . $catId . "' OR catid ='" . $catId2 . "'";
$db = JFactory::getDBO();
$db->setQuery($query);
$articles = $db->loadObjectList();
foreach($articles as $article){
echo '<br/><br/>ID:'. $article->id
.'<br/>Titulo: '. $article->title
.'<br/>Quantidade Comprada: '.$resultado[0]->add_compras_quantidade_comprada
.'<br/>Unidade de Medida: '.$resultado[0]->add_compras_unidade_de_medida
.'<br/>Preço Por Kilo/Unidade: '.$resultado[0]->add_compras_preco_por_unidade_kilo
.'<br/>Data da Compra: '.$resultado[0]->add_compras_data_da_compra
.'<br/>Total da Compra: '.$resultado[0]->add_compras_total_da_compra
.'<br/>Local da Compra: '.$resultado[0]->add_compras_local_da_compra
;
}
?>
What I want is to get the id of the article, article name, article category that are saved in the table #__content, and the other fields that are saved in the table #__cck_store_form_add_compras, but only articles with category id = 11 or category id = 12
When i use this code (above) I see the results however some of them appears repeated.
.
When i use this below, i see only the id of article and article name, but i cant see the other fields:
$catId = 11;
$catId2 = 12;
$query = "SELECT * FROM #__content WHERE catid ='" . $catId . "' OR catid ='" . $catId2 . "'";
$db = JFactory::getDBO();
$db->setQuery($query);
$articles = $db->loadObjectList();
.
if i use the code below, i can see only other fields, but i can't see the id of article and name of article:
$sql = "SELECT * FROM #__cck_store_form_add_compras";
$con = &JFactory::getDBO();
$con->setQuery($sql);
$resultado = $con->loadObjectList();
.
looking at the documentation I found this piece of code:
// Get a db connection.
$db = JFactory::getDbo();
// Create a new query object.
$query = $db->getQuery(true);
// Select all articles for users who have a username which starts with 'a'.
// Order it by the created date.
// Note by putting 'a' as a second parameter will generate `#__content` AS `a`
$query
->select($db->quoteName(array('a.*', 'b.username', 'b.name')))
->from($db->quoteName('#__content', 'a'))
->join('INNER', $db->quoteName('#__users', 'b') . ' ON (' . $db->quoteName('a.created_by') . ' = ' . $db->quoteName('b.id') . ')')
->where($db->quoteName('b.username') . ' LIKE \'a%\'')
->order($db->quoteName('a.created') . ' DESC');
// Reset the query using our newly populated query object.
$db->setQuery($query);
// Load the results as a list of stdClass objects (see later for more options on retrieving data).
$results = $db->loadObjectList();
I think it's exactly what I need, but i don't know how can I change to that fits my needs, someone could help me?
Sorry for my bad english, I'm using google translator to help me.
The best way to display a filtered list of SEBLOD contents is to use a SEBLOD search and display it through a menu item, a module or inside a SEBLOD content (use a CCK module or CCK list field).
In your SEBLOD search, you can set the 'article category id' as filter with "11,12" as default value so only articles in these 2 categories will be retrieved.
Please find the full answer to your question on SEBLOD forums http://www.seblod.com/community/forums/general-discussions/connecting-to-database#post22181
Thanks.
Sebastien.
With JTable::getInstance("content"); you may get data from Joomla content table and leave SEBLOD content for the query.
Example:
$query = "SELECT * FROM #__cck_store_form_produto";
$db = &JFactory::getDBO();
$db->setQuery($query);
$values = $db->loadObjectList();
foreach($values as $val){
echo $val->id; // The article ID is inside SEBLOD table too.
echo $val->your_field; // will write the value on a SEBLOD field called "your_field"
// now to get joomla values
$maindata =& JTable::getInstance("content");
$maindata->load($val->id);
echo $maindata->title; //will write article title
echo $maindata->alias; //will write article alias
echo $maindata->catid; //will write article Category ID
}
Or you can also use "INNER JOIN" between two tables just use the ID to link the values:
https://www.w3schools.com/sql/sql_join_inner.asp
Related
I am having an issue where I can not search for the exact data I want.
I can search the database fine and return all the values in the format I want. However i want to add in the ability to search by a company's name and also display those results in the same table. The companys Names are not stored in the databse but are in the Array session pulled from an API. We store the ID for each company in the database which is set as the Keys from the array.
I want to know if there is any way to get results given form the array as well as the database search.
I know i can do it with a foreach loop around all my SQL SELECT Statement, but the issue with that is if there is no match in the array it will not run any search, making the rest of the search very limited as searching by IP Address is one of the main reasons for this. the only way I think it is possible is adding a foreach loop into the SQL statement but I don't see how that is possible nor can i find any documentation on it online. please help!
Here is my code that runs based on an input from a search box sent by AJAX.
// Start sessions, Connected to DB set customer array
session_start();
$conn = connect();
$customers = $_SESSION['array'];
// if ajax posted search input
if($_POST){
$search = mysqli_real_escape_string($conn, $_POST['search']);
//set a second input for checking against customers array and format to give results
$input = $search
$input = strtolower($input);
$input = "'$input'";
$result = preg_grep($input, $customers);
$arrayk = array_keys($result);
//SELECT Search mysql Query
$matchingcompanies = "";
foreach($arrayk as $id)
{
$matchingcompanies .= "'" . $id . "' OR ";
}
$matchingcompanies = substr($matchingcompanies,0,strlen($matchingcompanies)-4);
$query1 = "SELECT * FROM IPV4_linked WHERE `companyId` = (" . $matchingcompanies . ") OR concat(ipv4Address, ipv4Subnet, hostName, owner, siteName) like '%" . $search . "%'";
$view4 = mysqli_query($conn, $query1);
//Table Title row
echo "<div><table name ='ipv4' class='tableparent'><td class='tabled'>IP Address</td><td class='table'>Subnet</td><td class='table'>PC Name</td><td class='table'>Owner</td><td class='table'>Customer</td><td class='table'>Customer Site</td></tr>";
//loops through search results and echo table
while ($row = mysqli_fetch_array($view4)){
$id = $row['companyId'];
$company = $customers[$id];
echo "<tr><td class='tabled'>". $row['ipv4Address']."</td><td class='table'>". $row['ipv4Subnet']."</td><td class='table'>".$row['hostName']."</td><td class='table'>". $row['owner']."</td><td class='table'>".$company."</td><td class='table'>".$row['siteName']."</td></tr>";
}
echo"</table></div>";
}
?>
Solved the issue by adding in a matchingcompanies field before and setting it to an exceeding long string with lots of OR's, i will limit the search to only start after 3 characters inputed so the string return is not so large.
//SELECT Search mysql Query
$matchingcompanies = "";
foreach($arrayk as $id)
{
$matchingcompanies .= "'" . $id . "' OR ";
}
$matchingcompanies = substr($matchingcompanies,0,strlen($matchingcompanies)-4);
$query1 = "SELECT * FROM IPV4_linked WHERE `companyId` = (" . $matchingcompanies . ") OR concat(ipv4Address, ipv4Subnet, hostName, owner, siteName) like '%" . $search . "%'";
$view4 = mysqli_query($conn, $query1);
I'm trying to get the category details from the joomla database for RSEvents. Can anyone shed any light on why this isn't working:
function _getCategorySlug($value) {
// Get a db connection.
$db = JFactory::getDbo();
// Create a new query object.
$query = $db->getQuery(true);
// Select all articles for users who have a username which starts with 'a'.
// Order it by the created date.
// Note by putting 'a' as a second parameter will generate `#__content` AS `a`
$query
->select($db->quoteName(array('a.*', 'b.id', 'b.ide')))
->from($db->quoteName('#__categories', 'a'))
->join('INNER', $db->quoteName('#__rseventspro_taxonomy', 'b')
. ' ON (' . $db->quoteName('a.id') . ' = ' . $db->quoteName('b.id') . ')')
->where($db->quoteName('b.ide') . ' = '.$db->quote($value));
// Reset the query using our newly populated query object.
$db->setQuery($query);
// Load the results as a list of stdClass objects (see later for more options on retrieving data).
$results = $db->loadObjectList();
}
I think this may help you. Using the below function you can get the categories of a particular event.
public function getCategories($id) {
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->clear()
->select($db->qn('id'))
->from($db->qn('#__rseventspro_taxonomy'))
->where($db->qn('type').' = '.$db->q('category'))
->where($db->qn('ide').' = '.$id);
$db->setQuery($query);
return $db->loadColumn();
}
Thanks! That was a great help and got me to where I need (with a little tweaking):
function getCategories($id) {
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->clear()
->select($db->qn('id'))
->from($db->qn('#__rseventspro_taxonomy'))
->where($db->qn('type').' = '.$db->q('category'))
->where($db->qn('ide').' = '.$id);
$db->setQuery($query);
$categories = $db->loadColumn();
return implode("_", $categories);
}
Also, would be great to find out what was wrong with my original query to get category's alias from the #__categories table
I'm back with another Laravel issue that I can't seem to crack with my procedural PHP, direct mysql_query background. It basically involves the Eloquent ORM over multiple tables.
I have 3 tables (Users, User_Profiles, and User_Answers). The Users table only keeps the very basics of the user (auto_incremented id, email, password), the User_Profiles table contains a few more details (image url, country, city, gender, etc) and belongs_to the User model. The User_Answers table is a list of answers given by the user and also belongs_to the User model.
What I would like to do is select all rows from the User_Profiles table where the city is the same as the city of the logged-in user, get their user_id's in an array and then compare the answers (from the User_Answers table) to the answers of the currently logged in User. The following is what I'm looking to do but in plain PHP and (for explanation purposes only) mysql_query. I apologize in advance for the awful code. I just need to be pointed in the right direction.
<?php
$link = mysql_connect("localhost", "xxx", "xxx");
$db = mysql_select_db('testersize', $link);
$id = 2;
$sql = "SELECT city FROM user_profiles WHERE user_id = $id";
$query = mysql_query($sql, $link);
$row = mysql_fetch_row($query);
$city = $row[0];
echo $city . "</br>";
$sql = "SELECT user_id FROM user_profiles WHERE city = '$city'";
$matches = mysql_query($sql, $link);
//
while($row = mysql_fetch_row($matches)){
$u_id = $row[0];
$sql = "SELECT books, movies, music FROM user_answers WHERE user_id = $u_id";
$query2 = mysql_query($sql, $link);
$row2 = mysql_fetch_row($query2);
echo "<h2>User Number: " . $u_id . "</h2>";
echo "your favorite books:" . $row2[0] . "</br>";
echo "your favorite movies:" . $row2[1] . "</br>";
echo "your favorite music:" . $row2[2] . "</br>";
}
?>
$user_profiles = User_Profiles::where('city','=',Auth::user()->city)->get();
foreach($user_profiles as $user_profile){
$id = array('user_id'=>$user_profile->user_id);
}
So that code will get you the user_profiles by city and then loop through them and put their in an array , im not sure what do you mean by match answers to users table though..
$user_ids = User_Profiles::where('city','=',Auth::user()->city)->lists('user_id');
$favorites = User_Answers::whereIn('user_id',$user_ids)->get();
foreach($favorites as $favorite)
{
echo $favorite->book;
echo $favorite->music;
echo $favorite->movie;
}
Ok Im getting a bit confused with arrays in php. This is the code
$search = "%" . $search ."%";
// Step 1: Establish a connection
$db = new PDO("mysql:host=localhost;dbname=########", "#########", "###########");
// Step 2: Construct a query
$query = "SELECT * FROM movies WHERE title LIKE " . $db->quote($search);
// Step 3: Send the query
$result = $db->query($query);
// Step 4: Iterate over the results
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
var_dump($row);
echo $row['title'];
}
// Step 5: Free used resources
$result->closeCursor();
$db = null;
Now when I search for say "Candyman" it returns 6 results, what I am tring to do is something like $row['title'][3] (So I can grab the title of the 3rd film in the array)
however this doesn't work $row['title']; displays all 6 titles how do I access the next level?
Below is a link to the search I am using
http://www.gorrors.com/moviesearch.php?search=candyman
Sorry for the newbie question but Im at a loss any help would be greatly appreciated.
Change the query, like, $query = "SELECT * FROM movies WHERE title LIKE " . $db->quote($search); use limit, $query = "SELECT * FROM movies WHERE title LIKE " . $db->quote($search) limit $start,$numbers; http://php.about.com/od/mysqlcommands/g/Limit_sql.htm
Try:
$rows = $result->fetchAll();
echo $row[3]['title']'
PDO::query:
http://www.php.net/manual/en/pdo.query.php
Which returns a PDOStatement object:
http://www.php.net/manual/en/class.pdostatement.php
...which has a method named fetchAll(), which returns an array of all the rows, which by default you can access using either numeric indexes or strings.
You can try Grouping all values by a single column :
//do not use * in your sql if you only need the imfromation of movie title
$query = "SELECT title FROM movies WHERE title LIKE " . $db->quote($search);
$result = $dbh->prepare($query);
$result->execute();
$row = $result->fetchAll(PDO::FETCH_COLUMN|PDO::FETCH_GROUP);
var_dump($row);
//$row['title'][2] will give the third result,not $row['title'][3]
I used this code to connect to a database and fetch results. This worked perfectly until i tried to work in another query to the images table to get associated images. I'm not very experienced with OO programming. So hopefully someone can see where ive gone wrong and help me out.
<?php
global $__CMS_CONN__;
$sql = "SELECT * FROM ecom_products";
$stmt = $__CMS_CONN__->prepare($sql);
$stmt->execute(array($id));
while ($row = $stmt->fetchObject()) {
$imagesql = "SELECT * FROM ecom_product_images where id = $row->id && where primaryImage = '1'";
$imagestmt = $__CMS_CONN__->prepare($sql);
$imagestmt->execute(array($id));
$imageName = $imagestmt->fetchObject();
echo ''.$row->productNm.''.$imageName;
}
?>
Modify your SQL query:
$imagesql = 'SELECT * FROM ecom_product_images where id = ' . $row->id . ' AND primaryImage = "1"';
Have you declared before $id variable ?
Have you got any errors with your code ?
If your primaryImage column is int type then skip apostrophes in query
primaryImage = 1
if enum then it is ok.
You need not OO programming, but SQL one.
SELECT p.*, imageName
FROM ecom_products p, ecom_product_images i
WHERE p.id = i.id AND primaryImage = 1;