How to use createElement() function in CodeIgniter? - php

I want to create an element in XML file using CodeIgniter, but every time I'm getting a fatal error: "Call to undefined method SimpleXMLElement::createElement()". Here is my code in Controller, View & Model class, what am I doing wrong?
Controller (xml_insert.php):
<?php
class Xml_insert extends CI_Controller {
function index() {
$this->load->model('xml_insert_model');
$data['rows'] = $this->xml_insert_model->getAll();
$this->load->view('xml_insert_view', $data);
}
function insert() {
$this->load->model('xml_insert_model');
$data['rows'] = $this->xml_insert_model->getAll();
foreach ($data['rows'] as $r) {
$path1 = $r->xml_file_path;
$xml = simplexml_load_file($path1);
$newAct = $_POST['activity'];
$root = $xml->firstChild;
$newElement = $xml->createElement('activity');
$root->appendChild($newElement);
$newText = $xml->createTextNode($newAct);
$newElement->appendChild($newText);
$xml->save('$path1');
$this->index();
}
}
}
View (xml_insert_view.php):
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<?php foreach ($rows as $r): ?>
<?php
$path1 = $r->xml_file_path;
$xml = simplexml_load_file($path1);
?>
<?php foreach ($xml->children() as $activity) : ?>
<?php echo "Activity : " . $activity . " <br />"; ?>
<?php endforeach; ?>
<?php endforeach; ?>
<form name="input" action="index.php/xml_insert/insert" method="post">
insert activity:
<input type="text" name="activity"/>
<input type="submit" value="send"/>
</form>
</body>
</html>
Model (xml_insert_model.php):
<?php
class Xml_insert_model extends CI_Model
{
function getAll()
{
$q = $this->db->get("XML");
if ($q->num_rows > 0) {
foreach ($q->result() as $row) {
$data[] = $row;
}
return $data;
}
}
}
XML file(sample.xml)
<?xml version="1.0"?>
<list>
<activity>swimming</activity>
<activity>running</activity>
<activity>Jogging</activity>
<activity>Theatre</activity>
<activity>Programming</activity>
<activity>driving</activity>
<activity>eating</activity>
</list>

I think you are trying to use functions of DomDocument
instead of
$xml = simplexml_load_file($path1);
Try
$xml = new DomDocument();
$xml->load($path1);
You can use addChild method of simpleXML if you prefer.

Related

How to display the values of database table in CodeIgniter?

I am creating a sample image test where in I want to display all of the images in a database table in CodeIgniter. The question is, how will I able to display the values of the database table using CodeIgniter?
Here is the code of my model:
<?php
class Image_model extends CI_Model{
/*Sample test function for the image dropdown list*/
public function getImages()
{
$query = $this->db->select('main_image_url'));
$this->db->get('product_master');
return $query->result();
}
/*End sample test function*/
}
?>
Here is the code of my controller:
<?php
if(! defined('BASEPATH')) exit('No direct script access allowed');
class Sample_image_dropdown extends MX_Controller{
public function __construct()
{
parent::__construct();
}
public function index()
{
$data['main_view'] = 'sample_view/image_dropdown_view';
$this->load->view('sample_view/image_dropdown_view', $data);
}
public function display_all_images()
{
$this->load->model('sample_model/image_model');
$data['images'] = $this->image_model->getImages();
$data['main_view'] = "sample_view/image_dropdown_view";
$this->load->view('sample_view/image_dropdown_view', $data);
}
}
?>
Here is the code of my view:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div>
<select name="" id="">
<option value="all">All Image</option>
<option value="with-image">With Image</option>
<option value="no-image">Without Image</option>
</select>
<input type="submit" value="Search">
</div>
<?php if (isset($images)):?>
<?php foreach ($images as $image):?>
<table>
<tr>
<td>
<th>
Images
</th>
</td>
</tr>
<tr>
<td>
<img src="<?php echo "$image->main_image_url";?>">
</td>
</tr>
</table>
<?php endforeach; ?>
<?php endif; ?>
</body>
</html>
You made a mistake in your model and in the controller, I mentioned the changes please note down and try,
Model File
class Image_model extends CI_Model{
/*Sample test function for the image dropdown list*/
public function getImages()
{
$query = $this->db->select('main_image_url');
$this->db->from('product_master');
$query = $this->db->get();
return $query->result();
}
/*End sample test function*/
}
Controller File,
public function __construct()
{
parent::__construct();
}
public function index()
{
$data['main_view'] = 'sample_view/image_dropdown_view';
$this->display_all_images();
}
public function display_all_images()
{
$this->load->model('sample_model/image_model');
$data['images'] = $this->image_model->getImages();
$data['main_view'] = "sample_view/image_dropdown_view";
$this->load->view('sample_view/image_dropdown_view', $data);
}
I assume you are storing images in directory at the time of uploading at
uploads/abc.png
then you can get data from db (which you are already doing)
and in view :
<img src="echo dir_path./$image->main_image_url;" />
or if you are storing whole path in DB then :
<img src="echo $image->main_image_url;" />

product detail from DB unique

I'm trying to show the detail of my product from a database, but i only want to show the product I clicked using the button "afficher le detail" right now it shows all app at once.
Any tips would be appreciated because I'm really lost on this one.
i don't know if i need to modify the index, class.app or product page.
this is what i got on my index.php
<?php
session_start();
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Applications</title>
</head>
<body>
<?php
include ("class.app.php");
$obj_app = new app();
include("class.client.php");
$obj_client = new client();
$btnajouterpanier=false;
if(isset($_SESSION['utilisateur'])){
if($obj_client->validate_login($_SESSION['utilisateur'],$_SESSION['motdepasse'])){
echo $obj_client->get_welcome_message($_SESSION['utilisateur']);
$btnajouterpanier=true;
}
}
if($_SERVER['REQUEST_METHOD'] === 'POST' && $_SERVER["CONTENT_TYPE"] === 'application/json'){
$data = json_decode(file_get_contents('php://input'),true);
$resultat = array('ajout' => FALSE);
$_SESSION['panier'][$_POST($data['id'])]+= 1;
$resultat['ajout'] = TRUE;
}
echo '<form name="frm_cat" action="index.php" method="POST">';
echo '<select name="lst_cat">';
$obj_app->get_cat_options();
echo '</select>';
echo '<input type="submit" name="btn_cat" value="Filtrer" />';
echo '</form>';
if(isset($_POST['lst_cat'])){
$obj_app->get_app_list($_POST['lst_cat'],$btnajouterpanier);
}else{
$obj_app->get_app_list('',$btnajouterpanier);
}
?>
<script src="jquery-3.2.1.min.js"></script>
<script src="panier.js"></script>
</body>
</html>
this is my class.app.php
<?php
class app{
private $dbh;
public function __construct(){
$this->dbh = new PDO('mysql:host=localhost;dbname=appstore','root','',array(PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ));
}
public function get_cat_options(){
$req = $this->dbh->prepare('SELECT * FROM cat ORDER BY titre');
$req->execute();
while($cat = $req->fetch()){
echo '<option value="'.$cat->id.'">'.$cat->titre.'</option>';
}
}
public function get_app_list($idcat="",$btnajouterpanier=false){
if ($idcat!=""){
$reqapp = $this->dbh->prepare("SELECT app.* FROM app JOIN appcat ON app.id=appcat.idapp WHERE appcat.idcat='".$idcat."'");
}else{
$reqapp = $this->dbh->prepare("SELECT app.* FROM app");
}
$reqapp->execute();
while($app = $reqapp->fetch()){
echo '<h3>'.$app->nom.'</h3><br/>';
echo '<img src="image/'.$app->image.'" /><br/>';
echo '<p>'.$app->description.'</p><br/>';
echo $app->prix.' $<br/>';
echo "<form name='btn_det' method='POST' action='produit.php'><input type='hidden' name='app_detail' value='".$app->id."' /><input type='submit' name='iddetail' value='Afficher le detail' data-id='".$app->id."'/></form>";
if($btnajouterpanier){
echo '<button type="button" class="btnajouterpanier" data-id="$app->id">Ajouter au panier</button>';
}
echo '<hr/>';
}
}
public function detail($iddetail){
$reqdet = $this->dbh->prepare('SELECT * FROM app where id = ?');
$reqdet->execute(array($iddetail));
while($app = $reqdet->fetch()){
echo '<h3>'.$app->nom.'</h3><br/>';
echo '<img src="image/'.$app->image.'" /><br/>';
echo '<p>'.$app->description.'</p><br/>';
echo $app->prix.' $<br/>';
}
}
}
?>
<script src="jquery-3.2.1.min.js"></script>
<script src="panier.js"></script>
and this is my produit.php page
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Document sans nom</title>
</head>
<body>
<?php
include ("class.app.php");
$obj_app = new app();
$obj_app->detail($_POST['iddetail'])
?>
</body>
</html>

How to search with pagination in codeigniter?

I want to do a simple search with pagination in codeigniter...
I dont really know how to do it..
My Model
public function testSearch()
{
$this->db->like('Profile_for', 'Myself');
$query = $this->db->get('users');
return $query->result();
}
My Controller
public function searcher()
{
$config['base_url']='http://localhost/index.php/controller/searcher';
$config['total_rows'] = $this->Model->testSearch()->num_rows();
$config['per_page'] = 5;
$config['num_links'] = 2;
$this->pagination->initialize($config);
$data['records'] = $this->Model->testSearch($config['per_page'], $this->uri->segment(3));
$this->load->view('pagination', $data);
}
I THINK MY CONTROLLER CODES ARE WRONG....
Please help me guyz....
Thanks in advance.....
1.Open routes.php under Config folder
2.Now create a file called pagination.php under controllers folder and paste the following code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Pagination extends CI_Controller
{
public function __construct() {
parent:: __construct();
$this->load->helper("url");
$this->load->model("Countries_Model");
$this->load->library("pagination");
}
public function index() {
$config["base_url"] = base_url() . "pagination";
$config["total_rows"] = $this->Countries_Model->record_count();
$config["per_page"] = 10;
$config["uri_segment"] = 2;
$this->pagination->initialize($config);
$page = ($this->uri->segment(2))? $this->uri->segment(2) : 0;
$data["results"] = $this->Countries_Model
->get_countries($config["per_page"], $page);
$data["links"] = $this->pagination->create_links();
$this->load->view("pagination", $data);
}
}
3.Create a file called countries_model.php under models folder and paste the following code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Countries_Model extends CI_Model
{
public function __construct() {
parent::__construct();
}
public function record_count() {
return $this->db->count_all("tblcountries");
}
public function get_countries($limit, $start) {
$this->db->limit($limit, $start);
$query = $this->db->get("tblcountries");
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
}
4.Lastly, create a file called pagination.php under views folder and paste the following code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8">
<title>Pagination with CodeIgniter</title>
</head>
<body>
<div id="container">
<h1 style="text-align:center">Page Navigation in PHP</h1>
<table width="400" border="1" align="center">
<tr>
<td width="100" bgcolor="#CCCCCC"><p>Country ID</p></td>
<td width="300" bgcolor="#CCCCCC">Country</td>
</tr>
<h1 style="text-align:center">Countries</h1>
<div id="body">
<?php
foreach($results as $data) {
?>
<tr>
<td><?php echo $data->CountryID ?></td>
<td><?php echo $data->Country ?></td>
</tr>
<?php
}
?>
</table>
<div style="text-align:center"><?php echo $links; ?></div>
</div>
</div>
</body>
</html>
This is code concept is same as your code.I hope this will help you
lot Thank you

Codeigniter displaying message

I dont know whats wrong with my code..
I am trying to display message from database, but i keep on getting error that $results are not defined.
Controller
public function getMessages()
{
$this->load->model('get_message');
$data['results']= $this->get_message->getMessage();
$this->load->view('home_view', $data);
}
Model
<?php
class get_message extends CI_Model{
function getMessage(){
$query = $this->db->query("SELECT * FROM messages");
return $query->result_array();
}
}
View
<!DOCTYPE html>
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>Home</h1>
<h2>Welcome <?php echo $username; ?>!</h2>
<div id="Main">
<?php
foreach($results->result() as $row){
echo $row ->id;
echo $row ->user_username;
echo $row ->text;
echo $row ->posted_at;
echo "<br/>";
}
?>
</div>
Logout
</body>
</html>
EDIT: here is what my code looks like now
Controller
public function index()
{
$this->load->model('get_message');
$data['results']= $this->get_message->getMessage();
$this->load->view('home_view', $data);
}
Model
<?php
class get_message extends CI_Model{
function getMessage(){
$query = $this->db->query('SELECT * FROM messages');
return $query->result_array();
}
}
view
<!DOCTYPE html>
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>Home</h1>
<div id="Main">
<?php
foreach($results as $row){
echo $row['id'];
echo $row['user_username'];
echo $row['text'];
echo $row['posted_at'];
echo "<br/>";
}
?>
</div>
</body>
</html>
This code works. if it doesn't you are doing something else wrong.
In the controller you send data as result
$data['result']= $this->get_message->getMessage();
Access in view like this:
foreach($result as $row){
}
You used as results in foreach instead of result
You are passing wrong element to the view.
$data['result']= $this->get_message->getMessage();
^
Should be
$data['results']= $this->get_message->getMessage();
^
Also in Model,
return $query->result();
Should be:
return $query;
And in view,
foreach($results as $row){
Should be
if ($results->num_rows()) { // IN CASE, THE RESULT HAS NO ROWS.
foreach($results->result() as $row) {
View :
Your code
foreach(**$results** as $row){
}
?>
change to
foreach(**$result** as $row){
}
?>
Try to get result in some other variable in your controller function -
public function getMessages()
{
$this->load->model('get_message');
$data['msgesult']= $this->get_message->getMessage();
$this->load->view('home_view', $data);
}
And then in view -
<div id="Main">
<?php
foreach($msgesult as $row){
echo $row['id'];
echo $row['user_username'];
echo $row['text'];
echo $row['posted_at'];
echo "<br/>";
}
?>
</div>
This should work.

Unable to insert data

I am a newbee to CodeIgniter and PHP. I am trying to insert data using a form and display it on the same view page using MVC pattern given by CodeIgniter. My View is:
<html>
<head>
<title>My Form</title>
</head>
<body>
<? echo form_open('books/input'); ?>
<? echo $id; ?>:
<? echo form_input('id'); ?>
</br>
<? echo $title; ?>:
<? echo form_input('title'); ?>
</br>
<? echo $body; ?>:
<? echo form_input('body'); ?>
</br>
<? echo form_submit('mysubmit','Submit!'); ?>
<? echo form_close(); ?>
</body>
</html>
and My Controller is:
<? class Books extends Controller{
function Books(){
parent::Controller();
}
function main(){
$this->load->model('books_model');
$data = $this->books_model->general();
//$this->load->view('books_main',$data);
}
function input(){
$this->load->helper('form');
$this->load->model('books_model');
$data = $this->books_model->general();
$this->load->view('books_input',$data);
}
} ?>
and My model is just returning the data entered by the user as:
<? class books_model extends Model{
function books_model(){
$this->load->helper('url');
}
function general(){
$data['id'] = 'id';
$data['title'] = 'title';
$data['body'] = 'body';
return $data;
}
} ?>
But when I try to access the form, it shows me an error quoting:
load->model('books_model'); $data = $this->books_model->general(); }
function input(){ $this->load->helper('form'); $this->load->model('books_model'); $data = $this->books_model->general();
$this->load->view('books_input',$data); } } ?>
404 Page Not Found
though I am accessing it with the same URL: localhost/CodeIgniter/index.php/books/input
How can I access this form and insert data successfully?
Every Class name should start with capital letter
like
class Books_model extends Model
and do type like this
$this->load->model('Books_model');
this could be the issue that you are getting "404 page not found".
Read some basic things that you have to follow from guide
http://ellislab.com/codeigniter/user-guide/general/models.html

Categories