remove html tags from result in CodeIgniter - resultset

I am getting result from database
$query = $this->db->query("Select Query");
return $query->result_array();
In controller
$this->output->set_content_type('application/json');
echo json_encode($query);
Now I want to remove all html tags from text inside array result in model.

You could just use the strip_tags function!

Related

With PHRETS v2, Can I Sort the Data Fields Alphabetically? (Had this when using V1)

This code works in terms of retrieving data:
<?php
date_default_timezone_set('America/Phoenix');
require_once("composer/vendor/autoload.php");
$config = new \PHRETS\Configuration;
$config->setLoginUrl('my_url')
->setUsername('my_user')
->setPassword('my_pass')
->setRetsVersion('1.7.2');
$rets = new \PHRETS\Session($config);
$connect = $rets->Login();
$system = $rets->GetSystemMetadata();
echo "Server Name: " . $system->getSystemDescription();
$property_classes = ['Property'];
foreach ($property_classes as $pc) {
// generate the DMQL query
$query = "(BedroomsTotal=1+),(MlsStatus=ACT,PND)";
$results = $rets->Search('Property', $pc, $query);
file_put_contents('MyFolder/Property_' . $pc . '.csv', $results->toCSV());
} //end for each property class
php?>
I would like to know how to sort the fields alphabetically, in order to keep fields in a predicable order, which could also be used in an SQL CREATE TABLE statement. I had this ability with v1.
I would also like to be able to loop through the data fields with a FOR EACH kind of statement, in order to create a customized field delimiter; a custom delimiter helps with avoiding import errors in cases where the delimiter also appears within the metadata, such as quotes and commas within remarks section.
Any help is much appreciated. :)

Pagination with PDO prepare statement

I am using David Carr's pagination class successfully for a whole-table call (i.e. where I'm calling all the rows in the table without filtering). After including my config and the class, I accomplish it this way:
$pages = new Paginator('3','p');
$stmt = $dbh->query('SELECT count(id) FROM sermons');
$row = $stmt->fetch(PDO::FETCH_NUM);
$total = $row[0];
//pass number of records to
$pages->set_total($total);
$results = $dbh->query('SELECT * FROM sermons ORDER BY date_preached DESC '.$pages->get_limit()); ?>
<div class="paging"><p><?php echo $total; ?> sermons found</p> <?php echo $pages->page_links();?></div>
<?php
foreach($results as $row)
Etc.
That works great, but now I'm trying to implement it for the result of user filtering via forms, and because I can't use an unnamed placeholder in the query, I'm trying to do it with a prepare (which I'm guessing is more secure anyway):
$pages = new Paginator('2','p');
$series = isset($_POST['series']) ? $_POST['series'] : false;
try {
$stmt = $dbh->prepare("SELECT count(id) FROM sermons WHERE series = ?");
$stmt->execute(array($series));
$row = $stmt->fetch(PDO::FETCH_NUM);
$total = $row[0];
//pass number of records to
$pages->set_total($total);
$results = $dbh->prepare("SELECT * FROM sermons WHERE series = ? ".$pages->get_limit());
$results->bindParam(1, $series, PDO::PARAM_STR);
$results->execute(array($series));
// the call
echo $total; ?> sermons found</p> <?php echo $pages->page_links();
When I first get to the results page from the form, it looks like everything is working properly. The call correctly identifies the number of sermons found for the search result, and displays the first two (or however many I set in the $pages statement). But when I click to another page result, no sermons display, the count is gone, and my !isset(found_rows) echo returns a zero as well.
I was originally thinking the issue was that at first I was trying to do this without binding. That is questionable, since without the pagination I can display the page without binding. In any case, I've been trying that with both bindValue and bindParam, but with no luck.
Am I mishandling something in the prepare statement? Or what?
Note: I have the results page displaying fine without the pagination with this:
$series = isset($_POST['series']) ? $_POST['series'] : false;
try {
$results = $dbh->prepare("SELECT * FROM sermons WHERE series = ?");
$results->execute(array($series));
// Etc.

Insert SQL statement error

Hi Im trying to insert data into a database using an insert statement. So basically, the user inputs data into a form and then once the submit button is clicked its meant to get the property_id of the table Property.
My code is this:
<?php
$id = intval($_GET['id']);
$query = mysql_query('SELECT * FROM review WHERE property_id="'.$id.'"');
if(isset($_POST['submit']))
{
$review = mysqli_real_escape_string($mysqli, $_POST['review']);
if(mysqli_query($mysqli, "INSERT INTO review(review) VALUES ('$review')"))
{
?>
<script>alert('Successfully Updated ');</script>
<?php
}
else
{
?>
<script>alert('Error...');</script>
<?php
}
}
?>
At the top of the page is my other code which is as followed:
<?php
include_once '../db/dbconnect.php';
$id = intval($_GET['id']);
$sql = 'SELECT* FROM property WHERE property_id="'.$id.'"';
$result = mysqli_query($mysqli, $sql);
$row=mysqli_fetch_array($result);
?>
The code above basically displays all the data for that individual property. Any help would be great.
There are several errors in your code:
$id = intval($_GET['id']);
$query = mysql_query('SELECT * FROM review WHERE property_id="'.$id.'"');
The mysql_* functions are deprecated in PHP 5, and totally removed in PHP 7. Don't use them!
Moreover, it's not possible to use mysql_* and mysqli_* functions together.
Yet another error: you are executing a SELECT query, but you never fetch the results!
Note: you don't need to concatenate $id. It makes the code harder to read with plenty of useless single and double quotes, and increases the likeliness of a typo. Just enclose the variables in a double-quoted string.
You are casting $id to an int value. If the field property_id is an integer, there is no need to put single quotes around $id in the query.
Updated snippet:
$id = intval($_GET['id']);
$query = mysqli_query($mysqli, "SELECT * FROM review WHERE property_id=$id") or die(mysqli_error($mysqli));
while($r = $query->fetch_assoc()) {
// do something here with the current record $r
}
Your code:
if(mysqli_query($mysqli, "INSERT INTO review(review) VALUES ('$review')"))
[...]
<script>alert('Error...');</script>
When developing, you should display (or write to a log file) the MySQL error message from each failing query. It will make debugging much easier:
<script>alert('Error: <?= mysqli_error($mysqli) ?>');</script>

$data = $query->row(); returns only one row

Im trying to list the results of my sql query (picking up all the movies from a category), but I cannot figure out how to get all the rows instead of only one.
Here's the code :
$this->load->database();
$sql = 'SELECT * FROM movies WHERE category = "'.$movies_category.'";';
$query = $this->db->query($sql);
$data = $query->row();
$this->response($data, 200);
I've tried :
while($row = mysql_fetch_assoc($query)){
$data = $query->row();
}
$this->response($data, 200);
And it doesn't work. Any suggestion ? Thank you !
$this->load->database();
$sql = 'SELECT * FROM movies WHERE category = "'.$movies_category.'";';
$query = $this->db->query($sql);
$data = $query->result();
To traverse the $data array:
foreach($data AS $row)
{
//to retrieve the data from each row.
$col1 = $row->col1;
$col2 = $row->col2;
}
Use result() instead of row(). result() will return an array of objects that are your results. Alternatively, you can useresult_array() which will resturn an array of arrays keyed according to your columns. Please refer to here for a better outline of the result() and row() methods.
Do you have a database configuration file? the load->database() requires it. Where is $movies_category coming from? This will let you iterate over your results.
$this->load->database();
$sql = 'SELECT * FROM movies WHERE category = "'.$movies_category.'";';
$query = $this->db->query($sql);
foreach ($query->result() as $row)
{
echo $row->column;
}
Where column corresponds with one of the values in the movies table.
I'm surprised nobody has mentioned the potential hazards of using variables (possibly user input) in your SQL. You should seriously consider using query bindings or the active record features of CodeIgniter to build safer queries.
Consider the following solution to your problem:
$this->load->database();
$sql = 'SELECT * FROM movies WHERE category = ?';
$query = $this->db->query($sql, array($movies_category));
// $data = $query->result(); // returns result as an array of objects
$data = $query->result_array(); // returns result as array
$this->response($data, 200);
I'm assuming this is for some sort of API? If so, consider using the result_array() method as it will probably be better suited for your needed output, and also really easy to convert into JSON:
$json_data = json_encode($data);
Hope that helps,
Cheers.
For your question row() return only one value its good for checking in ID and if you want get all the rows use result_array() or simple result()
You can try this code....
Model:
function get_movies($movies_category){
$this->db->where("category",$movies_category);
$query = $this->db->get("movies");
return $query->result_array();
}
Controller:
$this->data['movies'] = $this->'name of model'->get_movies('here is the movie categories');
View:
foreach($movies as $m){
print_r($m);
}
exit();
Note you can directly add the code in function in model to controller add this in your controller if you want directly...
$this->data['movies] = $this->db->get('movies')->result_array();

fetch image and text from database using joomla 2.5

i have one one issue in fetch image and text from database by module what to do for this issue and i add my table name and field name #__home_service_item this is my table name in that table two field one is image and image_name than i have one error for that question i display my error
Warning: Invalid argument supplied for foreach() in C:\wamp\www\Joomla_2.5.8-Stable-Full_Package\modules\mod_home\tmpl\default.php on line 40
please give me any clue for that problem i also add my code
<?php
defined('_JEXEC') or die('Restricted access');
$items = $params->get('items', 1);
$db =& JFactory::getDBO();
$query = "SELECT id
FROM #__home_service_item
WHERE published = '1'
ORDER BY id DESC";
$db->setQuery( $query, 0 , $items );
$rows = $db->loadObjectList();
foreach($rows as $row)
{
echo 'ID: '.$row->id.' </br>';
}
?>
please give one clue
do print_r($rows) and see if any records are returning from the database. I think that you have a problem with your query. If there are no results returning try enclosing your foreach statement with in a try catch or ignore warnings.
Also try to set $db->setQuery($query); instead of $db->setQuery( $query, 0 , $items );
If you just need one row result use $db->loadResult();