how to write OSClass custom query to fetch result from DB - php

I m new to OSClass(OpenSource Classifieds).
i know its similar to wordpress., but i'm not sure how to write my own query like how i do in wordpress.
The right side bar will have cities/regions as links to display advertisements from that city/region.
The City/Region name is passed through the URL, and as per requirement i need to change the URL as virtual subdomain.
For example if the user click city1, then the URL should be http://city1.domainname.com/ and results asusual.
I used htaccess to change the virtual subdomain redirection but stuck with city and region.
to determine whether its a city or region i need to do a check with the database, for that i need write a custom query to match the value with the table..
so that i can display the results regarding the city/region.
Any ideas for this problem i'm facing..
Thanks.

I suggest you take a look to the models inside ./oc-includes/osclass/models/ to search regions and cities and other common queries.

Here is how I did in one of my plugins:
$conn = DBConnectionClass::newInstance();
$data = $conn->getOsclassDb();
$comm = new DBCommandClass($data);
$db_prefix = DB_TABLE_PREFIX;
$query = "SELECT * FROM `{$db_prefix}t_item` WHERE b_active=1 AND b_enabled=1";
$result = $comm->query($query);
if ($result) {
$items = $result->result();
foreach ($items as $item) {
var_dump($item);
}
}

Related

Multiple MYSQL: PHP loops not working

I've been writing some code that essentially collects information based on schools and the user search input. Once the information is pulled up, I also query a database containing users to show how many are signed up at each school, and then another database containing files showing how many files have been uploaded from each school.
I imagine this would require a three tiered loop? If I query the school database and then the student database in succession it works great (Every school will have the appropriate number of students signed up displayed). However the problem is with the files. If I add in the file query, it will only show the first two results of the schools.
This leads me to believe that the file database query isn't correct and after testing a two tiered loop (this time will files instead of students) it appears to be the case. So, what am I doing wrong with the file database code? I copied it directly from the student database code so I haven't a clue why this one won't work. Here is the code that works:
mysql_select_db($database_geographic, $geographic);
$query_school = "SELECT * FROM geographic.school WHERE countryid='$countryid' AND stateid='$stateid' ORDER BY school_name ASC";
$school = mysql_query($query_school, $geographic) or die(mysql_error());
$totalRows_schools = mysql_num_rows($schools);
while ($row_school = mysql_fetch_assoc($school)) {
echo $row_school['school_name'];
echo $row_school['city_name'];
echo $row_school['state_name'];
echo $row_school['schoolid'];
$schoolid = $row_school['schoolid'];
mysql_select_db($database_user_information, $user_information);
$query_users = "SELECT COUNT(*) AS studentcount FROM users WHERE school_name= '$schoolid'";
$users = mysql_query($query_users, $user_information) or die(mysql_error());
while ($row_users = mysql_fetch_assoc($users)) {
echo $row_users['studentcount']; }
But if I throw in this third files loop statement it will not work.
mysql_select_db($database_files, $files);
$query_files = "SELECT COUNT(*) AS filecount FROM file_data WHERE school_id= '$schoolid'";
$files = mysql_query($query_files, $files) or die(mysql_error());
while ($row_files = mysql_fetch_assoc($files)) {
echo $row_files['filecount']; }
}
If I use the file query in place of the student query it will not work either. The problem must be with the file query but I can't figure it out. Any help would be awesome! Thanks!

Proper Query in RETS

I need a little assistance regarding to RETS. I have not worked in it before. I am stuck at a point.
Here is the code
$rets_modtimestamp_field = "LastTr_260";
$previous_start_time = "2013-01-01T00:00:00";
$listing_status = "Status_383";
$listing_price = "ListPr_276";
The original query that I got was
$query = "({$rets_modtimestamp_field}={$previous_start_time}+)";
I had to update the query to add listing status and listing price. I searched around over the internet and updated the query to this.
$query = "(ListPrice=ListPr_276),(ModificationTimestamp=LastTr_260),(ListingStatus=Status_383),(".$previous_start_time."+)";
This is where the query is being used..
$search = $rets->SearchQuery("Property", $class, $query, array('Limit' => 1000));
Any ideas why the query is returning no results? I feel there is something in reference to start time...I have no idea about it...
Any help would be highly appreciated.
Thanks and Cheers
Ahmad
The last condition in your query is missing the name in the name=value pair. So your query would translate to:
$query = "(ListPrice=ListPr_276),(ModificationTimestamp=LastTr_260),(ListingStatus=Status_383),(2013-01-01T00:00:00+)";
With the name-value pair:
$query = "(ListPrice=ListPr_276),(ModificationTimestamp=LastTr_260),(ListingStatus=Status_383),({$rets_modtimestamp_field}={$previous_start_time}+)";
These look like the type of RETS field names that come from Interealty - one of the major RETS vendors. There are five variations of RETS field names - the "SystemName", "StandardName", "LongName", "ShortName", and "DBName." You appear to be querying on the DBName, which usually is not queryable. Try the SystemName, which should always be queryable. Interealty uses numeric SystemNames, so your list price field, ListPr_176, would very likely have the SystemName "176". The part of the query for the price would then look more like "(176=0+)". The entire query code should probably look more like this:
$rets_modtimestamp_field = "260";
$previous_start_time = "2013-01-01T00:00:00";
$listing_status = "383";
$listing_price = "276";
$query = "(".$listing_price."=0+),(".$listing_status."=|A),(".$rets_modtimestamp_field."=".$previous_start_time."+)";
(I added a presumed lookup value for Listing Status of active)

How to populate a dropdown list from the database table in SugarCRM Community 6.5?

I've asked this question in the SugarCRM forums, but have been unable to find the answer as well. I am trying to populate a dropdown list in SugarCRM Community 6.5. I am using PHP5, MySQL and Apache. I created a custom dropdown list named 'oz_accounts' in the dropdown editor, created a new field in the custom module, Machines, that I built and associated the field with the Edit, Quick Create, Detail and List views based on the dropdown menu. Here is the code that I used, the result is a blank dropdown menu:
<?php
require_once('include/entryPoint.php');
$db = & DBManagerFactory::getInstance();
$myQuery = "SELECT name FROM accounts";
$Result = $db->query($myQuery);
$new_array = array();
while($row = $db->fetchByAssoc($Result)) {
$new_array[$row['key']] = $row['value'];
}
$GLOBALS['app_list_strings']['oz_accounts'] = $new_array;
Could someone please advise on what I'm doing incorrectly? I tested the query in phpmyadmin, and it retrieves the result i'm looking for, so I'm assuming that I must be making a mistake somewhere in the PHP code, possibly connecting to the database.
I posted directions on how to do this here some time ago: http://www.eggsurplus.com/content/populate-a-dropdown-from-the-database/
What you need to do is to create a custom util function and then tell your vardefs for that field to call the custom util function.
//custom/Extension/application/Ext/Utils/getAccounts.php
function getAccounts(){
static $accounts = null;
if(!$accounts){
global $db;
$query = "SELECT id, name FROM accounts";
$result = $db->query($query, false);
$accounts = array();
$accounts[''] = '';
while (($row = $db->fetchByAssoc($result)) != null) {
$accounts[$row['id']] = $row['name'];
}
}
return $accounts;
}
Name of the file doesn't matter. Make sure to include opening PHP tags. Run a Repair/Rebuild so that it builds custom/application/Ext/Utils/custom_utils.ext.php.
Then in the vardef definition for that field set the function to getAccounts:
'function' => 'getAccounts',
Repair/Rebuild will be needed after the vardef change.
the problems is in process definition the list not showing

php for loop query in magento

Hello all i am new but got so many helps from here. Thanks to all the people who helped.
Now i am facing a problem in magento e-commerce for making a loop query of top 10 point holders. Well i have made a separate database which contain the points .
my database is table : magentodatabase_points
Now here is total 3 colums -
1. ID
2. User_id
3. points
well now, here i would like to make a loop of top 10 members contain the highest points i have tried but failed so can anybody help me on that.
$resource = Mage::getSingleton('core/resource');
$readConnection = $resource->getConnection('core_read');
$table = $resource->getTableName('database_points');
$query = 'SELECT user_id FROM ' . $table . ' ORDER BY points DESC LIMIT 10';
$results = $readConnection->fetchAll($query);
well but after that i have tried with php for loop but that is not supporting for magento. So can anybody know what will be the loop code for magento.
Well, one more help would be greatful for me how do i get magento customer names from the using the top results of user_id because i will get only the results not the names.
Anyways that is not that important because i hope i will solve it by my own.
So, please if anyone can help me on the loop query of top 10 point holders.
Thanks in advace
and sorry for my bad english
If your table name and fields exist then you can just add this below your code:
foreach ($results as $result) {
$user_id = $result['user_id'];
echo $user_id; // or do whatever you wanted to with the variable
}
You should consider using Magento's Models to do this instead. There's an excellent tutorial series from Alan Storm on MagentoCommerce's website: http://www.magentocommerce.com/knowledge-base/entry/magento-for-dev-part-5-magento-models-and-orm-basics
For the customer's names you can try this in your loop:
foreach ($results as $result) {
$user_id = $result['user_id'];
$customer = Mage::getModel('customer/customer')->load($user_id);
print_r($customer);
}
May be because your field name has uppercase for u of user_id correctd "User_id" and you are using lowercase in your query.
else your method is fine to work with magento
for gettting customer, if User_id u get from this result is a customer id then $customer_data = Mage::getModel('customer/customer')->load(User_Id); would give u all detail of customer. you can fetch name from this using $customer_data->getName() method

magento make multiple database connections without losing array values

I am trying to make connections to two different databases so my script should work as follows
Find all orders for the current logged in customer where the order state is complete, it is a virtual product and it has a juno order id (this query works fine)
Collect all of order ids that have been found and store them in an array (this works fine)
now connect to sales_order_items and for each item that is part of an order id check to see if the database has a url download link,
if not i will connect to an api --
the problem is when i want to make my second connection i seem to lose all the values that are stored in the $orderIds array.
i have been looking for solutions but i am pretty new to the zend framework
any help would be much appreciated
my script is as follows
$conn = Mage::getSingleton('core/resource')->getConnection('core_write');
$result = $conn->query('select * from sales_flat_order WHERE customer_id='.$session->getCustomerId().' AND state="complete" AND is_virtual=1 AND juno_order_id!="null"');
$orderIds=array();
foreach ($result as $orderId)
{
$orderIds[]=$orderId[entity_id];
$itemsonOrder=$conn->query('select * from sales_flat_order_items WHERE order_id='.$order[entity_id]);
}
// value of first array $orderIds gets lost if i make annother connection using $conn
echo 'items on order';
print_r($itemsonOrder);
echo '<pre>';
print_r($orderIds);
echo '</pre>';
Well you aren't done with the first query yet when you are connecting with the second query. Go ahead and finish with the first query, then start the second one. Try something more like this...
$conn = Mage::getSingleton('core/resource')->getConnection('core_write');
$result = $conn->query('select * from sales_flat_order WHERE customer_id='.$session->getCustomerId().' AND state="complete" AND is_virtual=1 AND juno_order_id!="null"');
$orderIds=array();
foreach ($result as $orderId)
{
$orderIds[]=$orderId[entity_id];
}
foreach ($orderIds as $orderId)
{
$itemsonOrder=$conn->query('select * from sales_flat_order_items WHERE order_id='.$orderId);
}
Also, you should probably make sure you are using parameters when doing queries. Concatenating sql strings like that can be dangerous (leaves you open to vulnerabilities sometimes). For example,
$conn->query('select * from sales_flat_order_items WHERE order_id='.$orderId);
should be changed to
$conn->query('select * from sales_flat_order_items WHERE order_id=?', array($orderId));
Also, do you really need to "select *"??? I mean, just select the columns you need.

Categories