Welcome Guest, Not a member yet? Register   Sign In
A really simple question
#1

[eluser]daulex[/eluser]
Ok, I'm a real newbie to coding/developing (I'm a designer with 6 years behind my back in photoshop and xhmtl), but I am getting quite bored with it and I decided to learn php(and everything that comes with it like mysql and ajax stuff), thought to start up with php.com, but then found out about codeigniter.com and the CI framework, man this stuff rocks, can't wait to learn a bit more and push it as far as I could.

So, to begin with, I went and watched both the tutorials that you got on the main page, built the blog and it was working and yeah, everything is great, I'm getting the hang of functions and more or less how CI works, however I still don't know the basics and its obvious that its hard to run without being able to walk properly...

So I wanted to understand how to call content from the database one by one, I learned this way:
Code:
function index () {
    $data['title'] = "Galenko.co.uk - Our services";
    $this->db->where('title',$this->uri->segment(2));
    $data['query']= $this->db->get('pages');
    $this->load->view('services', $data);
    }
and the view:
Code:
<title><?=$title?></title>
<link rel="stylesheet" type="text/css" href="<?=base_url();?>system/application/views/css/style.css" media="screen, print" />
</head>
<body>
<? foreach($query->result() as $row): ?>
<h3>&lt;?=$row->title?&gt;</h3>
<p>&lt;?=$row->body?&gt;</p>
&lt;? endforeach;?&gt;

So there probably is a better(and/or faster) way to do it, the database structure I am using (no idea if this is the proper way or not) is:
Code:
-- phpMyAdmin SQL Dump
-- version 2.11.4
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: May 28, 2008 at 11:38 AM
-- Server version: 5.0.51
-- PHP Version: 5.2.5

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";

--
-- Database: `ci`
--

-- --------------------------------------------------------

--
-- Table structure for table `pages`
--

CREATE TABLE IF NOT EXISTS `pages` (
  `id` mediumint(9) NOT NULL auto_increment,
  `title` varchar(100) NOT NULL,
  `body` text NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

--
-- Dumping data for table `pages`
--

INSERT INTO `pages` (`id`, `title`, `body`) VALUES
(1, 'aboutus', 'about us page content'),
(2, 'services', 'services page content'),
(3, 'clients', 'clients page content'),
(4, 'contactus', 'contact us page content');

Oh wise ones please give me advice on what would be the best thing to do Big Grin
#2

[eluser]GSV Sleeper Service[/eluser]
I'm not sure what you're asking, but the example code looks fine for what you're trying to accomplish.
#3

[eluser]daulex[/eluser]
not really,
Code:
function index () {
    $data['title'] = "Galenko.co.uk - Our services";
    $this->db->where('title',$this->uri->segment(2));
    $data['query']= $this->db->get('pages');
    $this->load->view('services', $data);
    }

this looks at the uri to find out what I need, unless I set up a forward, it will display all results on index, understand now?

cheers
#4

[eluser]krwl[/eluser]
[quote author="daulex" date="1211994284"]not really,
this looks at the uri to find out what I need, unless I set up a forward, it will display all results on index, understand now?
[/quote]

sorry for my bad understanding, but what do you want to achieve from the script?

oh, and maybe you want to learn about CI model to separate database access from controller Smile
#5

[eluser]Pascal Kriete[/eluser]
So what you're saying is, you want blog/index/some-title and blog/some-title to both show the same thing. As they should.

Or am I missing something?
#6

[eluser]daulex[/eluser]
Sorry for being confusing guys, thanks for
#7

[eluser]daulex[/eluser]
My internet was bust yesterday Big Grin

Sorry for being confusing guys, thanks for trying Big Grin

All I am trying to achieve is to have a basic web site, simply 4 pages, with url's like:
/ci/index.php/site/aboutus
/ci/index.php/site/services
/ci/index.php/site/clients
/ci/index.php/site/contactus

and I can do that, everything works (via the foreach and $this->db->where('title',$this->uri->segment(2));, so the title of the page matches the second bit(from index.php) of the url of the page, thus if the title field in the db is services and the url bit is services, in the foreach the body bit gives me the body that matches the services, so I get the correct content. (look through the sql dump and links Smile

and that is awesome Smile you won't believe how happy I was when it did that, however I need to be able to make /ci/ the default directory (and I know how to do that), so what happens is, $this->db->where('title',$this->uri->segment(2)); does not work, cuz there is no second segment, so it gives me the whole content, like so:
Quote:aboutus
about us page content

services
services page content

clients
clients page content

contactus
contact us page content

That's not good Big Grin, so I thought, there must be a way to get the specific bit of data from the database some other way than $this->db->where('title',$this->uri->segment(2));... and also, there must be a better way to create output for 1 bit of information than foreach...

HELP Big Grin before you start ranting at the dumbness of the question and giving me the 2 second obvious solution: I'm a total newbie to php and ci, dont kill me Big Grin
#8

[eluser]gunter[/eluser]
you can do this by routing (look under routing) or with the _remap function (look for remapping function calls - in the docs)


another quick solution would be this:


Code:
function index()
{
   $this->_db_and_view("main"); //main
}

function aboutus()
{
   $this->_db_and_view("aboutus");
}

function clients()
{
   $this->_db_and_view("clients");
}

function contactus()
{
   $this->_db_and_view("contactus");
}

function _db_and_view($title) {

    $this->db->where('title',$title);
    $data['query']= $this->db->get('pages');
    $this->load->view('services', $data);
    }
#9

[eluser]daulex[/eluser]
pasted that into site.php (which is my controller) and:
Code:
<div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">

<h4>A PHP Error was encountered</h4>

<p>Severity: Notice</p>
<p>Message:  Undefined variable: title</p>
<p>Filename: views/services.php</p>
<p>Line Number: 5</p>

</div>
#10

[eluser]Pascal Kriete[/eluser]
gunter's snippet doesn't define the $data['title'] variable anymore. So you have to add that line to the _db_and_view function:
Code:
$data['title'] = "Galenko.co.uk - ".$title;




Theme © iAndrew 2016 - Forum software by © MyBB