How to display image from database codeigniter - php

I am trying to display image from database using codeigniter. When a user search for a location, the information will display out including images. But I fail to display out the images. I have saved my image at file outside application. Here is my code.
//view.php
<style>
#searchbutton{
position: absolute;
left:300px;
top:30px;
}
fieldset {
background-color:#EFEAEA;
margin: 0px 0px 10px 0px;
padding: 20px;
border-radius: 1px;
width:900px;
margin-left:220px;
margin-top:-10px;
}
#user{
font-style:italic;
font-size: 12px;
text-align:right;
}
#titlereview {
font-style: italic;
font-size:20px;
}
#review {
font-size:16px;
}
</style>
<?=form_open_multipart('viewreview/view');?>
<?php $search = array('name'=>'search',);?>
<div id = "searchbutton">
<?=form_input($search);?><input type=submit value="Search" /></p>
</div>
<?=form_close();?>
<div class = "tablestyle">
<fieldset>
<?php foreach ($query as $row): ?>
<div id = "user">
User: <?php echo $row->name; ?><br>
Visited time: <?php echo $row->visitedtime; ?><br>
</div>
<div id = "titlereview">"<?php echo $row->titlereview; ?>"<br></div>
<div id = "review"><?php echo $row->yourreview; ?><br></div>
<div id = "image"><?php echo '<img src="data:image/jpeg, $row[images]"/>' ?<br><hr><br></div>
<?php endforeach; ?>
</fieldset>
</div>
//controller
<?php
class viewreview extends CI_Controller {
public function view($page = 'viewreview') //writereview page folder name
{
$this->load->model('viewreview_model');
$data['query'] = $this->viewreview_model->get_data();
$this->load->vars($data);
if ( ! file_exists('application/views/viewreview/'.$page.'.php')) //link
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = 'View Review';
//$data['title'] = ucfirst($page); // Capitalize the first letter
$this->load->helper('html');
$this->load->helper('url');
$this->load->helper('form');
$this->load->view('templates/header', $data);
$this->load->view('viewreview/'.$page, $data);
$this->load->view('templates/footer', $data);
}
}
?>
//model
<?php
class viewreview_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function get_data()
{
$match = $this->input->post('search');
$this->db->like('sitename',$match);
$this->db->or_like('titlereview',$match);
$this->db->or_like('yourreview',$match);
$this->db->or_like('suggestion',$match);
$query = $this->db->get('review'); //pass data to query
return $query->result();
}
}
?>

by default Code-igniter will return data in the form of std-object. you need to access that with -> (arrow).
e.g. jd reply in comment
<?php echo '<img src="data:image/jpeg,'.$row->images.'" />' ?>
or you need to pass parameter 'array' in function in your model
use this in your model : return $query->result('array'); return (Array)
Instance : return $query->result(); return (Std-Object)

Related

How can I successfully edit in my update php code?

I'm looking for solution on how to edit the inside of my modal -the title and the note. Below is my code, I want to edit inside the button which is in modal. After successfully updated the data, I want to display the updated data by calling the homepage (hh.php) which is in my code.
Below is my code:
<?php
if(isset($_GET['id'])){
$con=mysqli_connect("localhost","root","","task");
$qi=mysqli_query($con,"SELECT * FROM note WHERE id = ".$_GET['id']);
while($row=mysqli_fetch_assoc($qi)){
$tit = $row['title'];
$not = $row['note'];
}
}
?>
<html>
<head>
<script src="jquery-1.10.2.min.js"></script>
<style>
body
{
margin:0;
}
.submitted{
margin:0px;
}
.modal
{
width:100%;
height:100%;
position:fixed;
top:0;
display:none;
}
.modal_close
{
width:100%;
height:100%;
background:rgba(0,0,0,.8);
position:fixed;
top:0;
}
.close
{
cursor:pointer;
}
.note{
text-align:center;
}
#note{
font-family: Javanese text;
}
.call_modal{
font-family: myFirstFont;
}
.modal_main
{
width:50%;
height:400px;
background:#fff;
z-index:4;
position:fixed;
top:16%;
border-radius:4px;
left:24%;
display:none;
-webkit-animation-duration: .5s;
-webkit-animation-delay: .0s;
-webkit-animation-fill-mode: both;
-moz-animation-fill-mode: both;
-o-animation-fill-mode: both;
-webkit-backface-visibility: visible!important;
-webkit-animation-name: fadeInRight;
}
#-webkit-keyframes fadeInRight{0%{opacity:0;-webkit-transform:translateX(20px)}100%{opacity:1;-webkit-transform:translateX(0)}}
::-webkit-input-placeholder{
font-size: 13.4px;
}
button
{
padding:20px;
border-radius:5px;
background:#808080;
border:none;
font-size:18px;
color:#fff;
margin:8%;
}
</style>
<script>
$(document).ready(function(){
$(".call_modal").click(function(){
$(".modal").fadeIn();
$(".modal_main").show();
});
});
$(document).ready(function(){
$(".close").click(function(){
$(".modal").fadeOut();
$(".modal_main").fadeOut();
});
});
$(document).ready(function(){
$(".submitted").click(function(){
$(".modal").fadeOut();
$(".modal_main").fadeOut();
});
});
</script>
</head>
<body>
<button class="call_modal" style="cursor:pointer;"> Edit Task </button>
<div class="modal">
<div class="modal_close close"></div>
<div class="modal_main">
<div class="note"> <?php
$nameErr = $emailErr = $genderErr = $websiteErr = "";
$name = $email = $gender = $comment = $website = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["comment"])) {
$comment = "";
} else {
$comment = test_input($_POST["comment"]);
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<div id="name">Task Name:<textarea name="title" rows="1.8" cols="20" style="margin-top: 50px"><?php echo $tit;?></textarea></div>
<textarea name="note" rows="15" cols="90" style="margin-top: 20px" placeholder="Note"><?php echo $not;?></textarea>
<br><br>
<input type="submit" name="submit" class="submitted" value="Submit">
<?php
$con=mysqli_connect("localhost","root","","task");
if(isset($_POST['submit'])){
$message=$_POST['note'];
$title=$_POST['title'];
$qw='UPDATE note SET title = $title, note = $message';
mysqli_query($con,$qw);
$r="SELECT * FROM note";
$result = mysqli_query($con, $r);
while($row = mysqli_fetch_assoc($result)){
if($row['title']==$_POST['title']){
header("location:hh.php");
}
else{
echo 'Title already exist!';
}
}
}
?>
</form>
<img src="i783wQYjrKQ.png" class="close" style="line-height: 12px;
margin-top: 1px;
margin-right: 2px;
position:absolute;
top:0;
right:0;">
</div>
</div>
</body>
</html>
Taking a quick look at the code, you are leaving yourself wide open to a SQL injection attack.
<?php
if(isset($_GET['id']))
{
$con = mysqli_connect("localhost","root","","task");
$qi = mysqli_query($con, "SELECT 'title', 'note' FROM note WHERE id = ".$_GET['id']);
$row = mysqli_fetch_assoc($qi))
$tit = $row['title'];
$not = $row['note'];
}
?>
I'd look at using PHP PDO library to safe guard against using values passed in from a web page directly in SQL code.
I'd use something more akin to:
$host = 'localhost';
$db = 'task';
$user = 'root';
$pass = '';
$charset = 'utf8mb4';
$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
try {
$pdo = new PDO($dsn, $user, $pass, $options);
} catch (\PDOException $e) {
throw new \PDOException($e->getMessage(), (int)$e->getCode());
}
$stmt = $pdo->prepare('SELECT title, note FROM note WHERE id = :id');
$stmt->execute(['id' => $_GET['id']]);
$row = $stmt->fetch();
$tit = $row['title'];
$not = $row['note'];
Obviously the connection details etc would be held in a separate external file such that they don't have to be included with every PHP file where DB access is required.

Codeigniter login page won't work

I'm new to CodeIgniter and I've been trying to make a login page, I've already made a register page using a db for the users, but the login page just won't work.
When I click the login button, the page refreshes. I've tried to put many die('sss'); in the controller to figure out what am I doing wrong, but no 'sss' appeared on the screen. Here is my code. Can you please tell me what I'm doing wrong?
This is my controller:
public function login() {
$this->load->view('navbar');
$this->load->view('header');
$this->load->view('login');
$this->load->view('footer');
$password = $this->input->post('password');
$email = $this->input->post('email');
$result = $this->signup_model->login($email, $password);
if (isset($_POST['login'])) {
if ($this->form_validation->run() == FALSE) {
$this->session->set_flashdata('message', '<div class="alertmsg">Something is wrong!</div>');
redirect('users');
} else
if ($result) {
$sess_array = array();
foreach ($result as $row) {
$sess_array = array('id' => $row->id, 'email' => $row->email);
$this->session->set_userdata('logged_in', $sess_array);
}
return TRUE;
} else {
$this->form_validation->set_message('login', 'Invalid email or password');
return false;
}
}
}
View:
<div class="loginContainer">
<div class="loginWrapper">
<?php echo validation_errors(); ?>
<?php echo form_open('users/login'); ?>
<label for='email'>Email</label><div class='form_error'><?= form_error('email')?></div>
<input class='form-control1' name='email' placeholder='Email' type='text' value='<?php echo set_value('email'); ?> ' />
<span class='text-danger'><?php echo form_error('email'); ?> </span>
<label for='password'>Password</label><div class='form-error'><?= form_error('password') ?> </div>
<input class='form-control1' name='password' placeholder='Password' type='password' />
<span class='text-danger'><?php echo form_error('password'); ?></span>
<input type="submit" style='margin-left: 40%; margin-right: 40%; margin-top: 9%; text-align: center; width: 20%; ' value="Login"/>
</div>
Model:
public function login($email, $password) {
$this->db->select('id, email, password');
$this->db->from('register');
$this->db->where('email', $email);
$this->db->where('password', MD5($password));
$this->db->limit(1);
$query = $this->db->get();
if ($query->num_rows() == 1) {
return $query->result();
} else {
return false;
}
}
replace,
<input type="submit" style='margin-left: 40%; margin-right: 40%; margin-top: 9%; text-align: center; width: 20%; ' value="Login"/>
with,
<input type="submit" style='margin-left: 40%; margin-right: 40%; margin-top:9%; text-align: center; width: 20%; ' name='login' value="Login"/>
you missed name attribute.

How to update and delete data from table?

I'm new to codeigniter and I want to update and delete a data from the table. Can someone help solve this?I've tried other options but wasn't successful.
Here's my view:
<script>
function show_confirm(act) {
if (act == "edit")
var r = confirm("Do you really want to edit this?");
else
var r = confirm("Do you really want to delete this?");
if (r==true){
window.location="<?php echo base_url(); ?>site/"+act;
}
}
</script>
<style>
select{
position: absolute;
left: 35%;
height: 5%;
width: 33%;
border-radius: 5px;
font-family: arial;
}
table {
background: #333;
width: 700px;
border-collapse: 1px;
font-family: arial;
position: absolute;
top: 33%;
left: 25%;
color: #777;
}
th {
padding-top: 10px;
padding-bottom: 10px;
}
td {
background: #999;
padding: 10px;
text-align: center;
font-size: 12px;
}
.data {
color: #555;
}
.data:hover {
background: #FFF;
}
#action {
font-weight: bold;
color: #444;
text-decoration: none;
}
#action:hover {
color: #FFF;
}
</style>
</head>
<body>
<header>
<div class="menu">
<h1 class="sis">SIS</h1>
<p class="welcome">Welcome!&nbsp &nbsp| &nbsp</p>
<p class="logout">Logout</p>
<nav class="navi">
<ul>
<li>Home</li>
<li>About
<ul class="about">
<li>Vision/Mission</li>
<li>History</li>
</ul>
</li>
<li>Admission</li>
<li>Calendar</li>
<li>Students
<ul class="stud">
<li>Grade I</li>
<li>Grade II</li>
<li>Grade III</li>
<li>Grade IV</li>
<li>Grade V</li>
<li>Grade VI</li>
<li>Academic Records</li>
</ul>
</li>
<li>Teachers
<ul class="teach">
<li>Gradesheet</li>
<li>Lesson Plan</li>
</ul>
</li>
<li>Contact Us</li>
</ul>
</nav>
</div>
</header>
<header>
<div class="container">
<select>
<option placeholder="section"></option>
<option value="section">Section I</option>
<option value="section">Section II</option>
<option value="section">Section III</option>
<option value="section">Section IV</option>
<option value="section">Section V</option>
<option value="section">Section VI</option>
</select>
<table>
<tr id="field">
<th scope="col"> ID </th>
<th scope="col"> First Name </th>
<th scope="col"> Last Name </th>
<th scope="col"> Contact # </th>
<th scope="col"> Address </th>
<th scope="col" colspan="2"> Action </th>
</tr>
<?php foreach ($user_list as $u_key) { ?>
<tr class="data" >
<td><?php echo $u_key->ID; ?></td>
<td><?php echo $u_key->FNAME; ?></td>
<td><?php echo $u_key->LNAME; ?></td>
<td><?php echo $u_key->CONTACT; ?></td>
<td><?php echo $u_key->ADDRESS; ?></td>
<td width="40" align="left"><a id="action" href="#" onClick="show_confirm('edit',<?php echo $u_key->ID; ?>)">Edit</a></td>
<td width="40" align="left"><a id="action" href="#" onClick="show_confirm('delete',<?php echo $u_key->ID; ?>)">Delete</a></td>
</tr>
<?php } ?>
</table>
</div>
</header>
</body>
</html>
My Controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Site extends CI_Controller {
public function index(){
$this->home();
$this->load->helper('url');
}
public function home(){
$data['title'] = "Home";
$this->load->view("view_main");
}
function vismis(){
$data['title'] = "Vismis";
$this->load->view("view_vismis");
}
function history(){
$data['title'] = "History";
$this->load->view("view_history");
}
function admission(){
$data['title'] = "Admission";
$this->load->view("view_admission");
}
function contact(){
$data['title'] = "Contact";
$this->load->view("view_contact");
}
function calendar($year = null, $month = null){
$data['title'] = "Calendar";
if(!$year) {
$year = date('Y');
}
if(!$month) {
$month = date('m');
}
$this->load->model('cal_model');
if ($day = $this->input->post('day')) {
$this->cal_model->add_cal_data(
"$year-$month-$day",
$this->input->post('data')
);
}
$data['calendar'] = $this->cal_model->generate($year, $month);
$this->load->view("view_calendar",$data);
}
public function gradeI(){
$this->load->model('stud_model');
$data['title'] = "gradeI";
$data['user_list'] = $this->stud_model->get_users();
$this->load->view('view_students', $data);
}
function add_stud(){
$this->load->model('stud_model');
$udata['fname'] = $this->input->post('fname');
$udata['lname'] = $this->input->post('lname');
$udata['contact'] = $this->input->post('contact');
$udata['address'] = $this->input->post('address');
$res = $this->stud_model->insert_user_to_db($udata);
if ($res){
header('location:'.base_url()."site/gradeI");
}
}
public function edit() {
$this->load->model('stud_model');
$data['title'] = "edit";
$id = $this->uri->segment(3);
$data['user'] = $this->stud_model->getById($id);
$this->load->view('view_editstud', $data);
}
public function update_stud() {
$mdata['fname'] = $_POST['fname'];
$mdata['lname'] = $_POST['lname'];
$mdata['contact'] = $_POST['contact'];
$mdata['address'] = $_POST['address'];
$res = $this->stud_model->update_info($mdata, $_POST['id']);
if($res) {
header('location'.base_url()."site/gradeI");
}
}
public function delete() {
$this->load->model('stud_model');
$id = $this->uri->segment(3);
$res = $this->stud_model->del_stud($id);
if ($res) {
header('location:'.base_url()."site/gradeI");
}
}
function gradeII(){
$this->load->model('stud_model');
$data['title'] = "gradeII";
$data['user_list'] = $this->stud_model->get_users();
$this->load->view('view_stud_two',$data);
}
function gradeIII(){
$data['title'] = "gradeIII";
$this->load->view('view_students');
}
function gradeIV(){
$data['title'] = "gradeIV";
$this->load->view('view_students');
}
function gradeV(){
$data['title'] = "gradeV";
$this->load->view('view_students');
}
function gradeVI(){
$data['title'] = "gradeVI";
$this->load->view('view_students');
}
function acadrecs(){
$data['title'] = "acadrecs";
$this->load->view('view_acadrecs');
}
public function enroll(){
$data['title'] = "enroll";
$this->load->view('view_enroll');
}
}
and my Model:
<?php
class Stud_model extends CI_Model
{
function __construct()
{
parent::__construct();
$this->load->database("sis");
}
public function get_users() {
$query = $this->db->get('students');
return $query->result();
}
public function insert_user_to_db($udata) {
return $this->db->insert('students', $udata);
}
public function getById($id) {
$query = $this->db->get_where('students', array('id'=>$id));
return $query->row_array();
}
public function update_info($data, $id) {
$this->db->where('ID', $id);
return $this->db->update('students', $data);
}
public function del_stud($id) {
$this->db->where('ID', $id);
return $this->db->delete('students');
}
}
nothing happens when I click on delete.
On the edit link...
The view:
<form method="post" action="<?php echo base_url();?>site/update_stud">
<?php extract($user); ?>
<table>
<tr>
<th scope="row">Enter your first name</th>
<td><input type="text" name="fname" size="20" value="<?php echo $fname; ?>" /></td>
</tr>
<tr>
<th scope="row">Enter your last name</th>
<td><input type="text" name="lname" size="20" value="<?php echo $lname; ?>" /></td>
</tr>
<tr>
<th scope="row">Enter your contact number</th>
<td><input type="text" name="contact" size="20" value="<?php echo $contact; ?>" /></td>
</tr>
<tr>
<th scope="row">Enter your address</th>
<td><textarea name="address" rows="5" cols="20"><?php echo $address; ?> </textarea></td>
</tr>
<tr>
<td><input type="hidden" name="id" value="<?php echo $id; ?>" />
<input type="submit" name="submit" value="Update" /></td>
</tr>
</table>
This error will show up:
Enter your first name A PHP Error was encountered
Severity: Notice
Message: Undefined variable: fname
Filename: views/view_editstud.php
Line Number: 60
" />
and so on with other input types.
Firstly, neither of the javascript confirm links will work because you're not actually passing the id through with the act:
<script>
function show_confirm(act, id) {
if (act == "edit") {
var r = confirm("Do you really want to edit this?");
} else {
var r = confirm("Do you really want to delete this?");
}
//Just FYI, the above can be written like this:
//var r = (act == 'edit') ? confirm("Do you really want to edit this?") : confirm("Do you really want to delete this?");
//or even
//var r = confirm("Do you really want to "+act+" this?");
if (r == true) {
window.location = "<?php echo base_url(); ?>site/" + act + "/" + id;
}
//Another FYI, the above could be written like this:
// !r || window.location = "<?php echo base_url(); ?>site/" + act + "/" + id;
}
</script>
Obviously, you can delete the comments.
Secondly, you don't need to use $this->uri->segment() to pass params to a controller method as codeigniter will automatically pass them (Unless you're using URI ROUTING), you can instead:
public function delete($id)
{
$this->load->model('stud_model');
$res = $this->stud_model->del_stud($id);
//There is no point in this if statement as it will just display a
//blank page if $res evaluates to FALSE
if ($res) {
//As you aren't using a variable below you might as well use single quotes
//Also, instead of using header('Location') you should use redirect() with codeiginiter
redirect('site/gradeI');
}
}
Lastly, you should really think about validation e.g. codeigniters built in Form_validation Library.
Hope this helps!

How to edit and delete users from database in Codeigniter

I don't know how to do a edit and delete function in codeigniter.. I really need it for the admin side of my system.. I really need help.. I want to delete the id in my table named user_acc and edit the whole column
Model:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Mod_admin extends CI_Model{
function __construct()
{
parent::__construct();
}
public function user_acc(){
$this->db->select('*');
$this->db->from('user_acc');
$query = $this->db->get();
return $query->result();
}
public function delete()
{
$this->db->where('id');
$this->db->delete();
}
}
Admin:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Admin extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->Model('Mod_admin');
}
public function manageuser(){
$this->load->view('template/adminhead');
$data['user_acc'] = $this->Mod_admin->user_acc();
$this->load->view('manageuser', $data);
}
public function delete_product() {
$this->Mod_admin->delete();
$this->session->set_flashdata('message', '<p>Product were successfully deleted!</p>');
redirect('manageuser');
}
}
View:
<html>
<title>Manage User</title>
<body>
<style>
.shit{
border: 1px solid;
border-collapse: collapse;
}
.shit td{
border: 1px solid;
border-collapse: collapse;
padding: 7px;
}
.shit td tr{
border: 1px solid;
}
.shit td th{
border: 1px solid;
}
</style>
<div id="container">
<table class="shit">
<tr>
<th><strong>ID</strong></th>
<th><strong>Firstname</strong></th>
<th><strong>Lastname</strong></th>
<th><strong>Username</strong></th>
<th><strong>Password</strong></th>
<th><strong>E-mail</strong></th>
<th><strong>Contact</strong></th>
<th><strong>Edit</strong></th>
<th><strong>Delete</strong></th>
</tr>
<?php
foreach($user_acc as $row){
echo
'<tr>
<td>'.$row->id.'</td>
<td>'.$row->firstname.'</td>
<td>'.$row->lastname.'</td>
<td>'.$row->username.'</td>
<td>'.$row->password.'</td>
<td>'.$row->email.'</td>
<td>'.$row->contact.'</td>
<td>'.''.'Edit'.'</td>
<td>'.''.'Delete'.'</td>
</tr>';
}
?>
</table>
</div>
</body>
</html>]
How View looks like
Please help me to edit and delete users thanks
For updating
In controller:
$result = array( 'column1'=>$val1,'column2'=>$val2,'column3'=>$val3);
$this->model_name->updateEvent( $id , $result );
In model:
public function updateEvent($id, $result){
$this->db->where('db_table_column', $id);
$this->db->update('table_name', $result);
}
For deleting
In controller:
$this->model_name->deleteEvent( $id );
In model:
public function deleteEvent( $id )
{
$this->db->where('db_table_column', $id);
$this->db->delete('table_name');
}

Multicolored divs

my site which is a search engine returns many many results with a foreach loop as such:
foreach ($xml->channel->item as $result) {
$ltitle = $result->title;
$ldesc = $result->description;
$url = $result->displayUrl;
$link = $result->link;
if (strlen($ltitle) > 60)
{
$title = substr($ltitle,0,60).'...' ;
}
else
{
$title = $ltitle;
}
if (strlen($ldesc) > 195)
{
$desc = substr($ldesc,0,195).'...' ;
}
else
{
$desc = $ldesc;
}
echo "
<br>
<div class='resultbox'>
<a class='normal' style='text-decoration:none;font-size:huge;font-weight:bold' href='$link'>$title</a><br>
<div style='padding-top:3px;padding-bottom:4px;width:580px;'>
<font style='text-decoration:none;font-size:small;font-family:Arial;'>$desc<br></font></div>
<a style='text-decoration:none;' href='$link'><font style='text-decoration:none;font-size:small;color:green;font-weight:bold;'>$url<br></font></a>
</div>
";
}
And the resultbox class above styles all of the results with this
.resultbox
{
height:auto;
width:600px;
background-color:transparent;
font-size:19px;
padding:10px;
padding-left: 30px;
padding-right: 30px;
border-left: 6px solid #333;
}
.resultbox:hover
{
border-left: 8px solid #555;
}
The border-left color is what i want changed, i would like it to generate or to style randomly off of a list of colour codes so the results, insead of being all #333 can be #333 #555 #999 and so on..... any ideas?
If u have no problems using JS , You can certainly do this :
$(document).ready(function () {
$('.resultbox').mouseenter(function() {
var randomColor = Math.floor(Math.random()*16777215).toString(16);
$('.resultbox').css("border-left", " 8px solid #"+randomColor);
});
});
change <div class='resultbox'> to <div class='resultbox random-color-".rand(1,YOUR_COLOR_LIMIT)."'> AND define colors like
.random-color-1 {
border-left: 8px solid #555;
}
.random-color-2 {
border-left: 8px solid #555;
}
.....
.random-color-YOUR_COLOR_LIMIT {
border-left: 8px solid #555;
}
change
<div class='resultbox'>
to
<div class='resultbox' style='border-left-color:$yourColorInCssFormat;'>
the style attribute overrides the css from class.
set $yourColorInCssFormat to the color you wish to have for the div. for example: $yourColorInCssFormat = '#999';
You can use inline style for that. Or alternatively you can user nth-child selector of css to repeat the border-color scheme something like this:
.resultbox:nth-child(n+1):hover {
}
.resultbox:nth-child(2n+1):hover {
}
.resultbox:nth-child(3n+1):hover {
}
First off, try this out for your foreachloop:
<?php foreach ($xml->channel->item as $result): ?>
<?php
$ltitle = $result->title;
$ldesc = $result->description;
$url = $result->displayUrl;
$link = $result->link;
if (strlen($ltitle) > 60){
$title = substr($ltitle,0,60).'...' ;
}else{$title = $ltitle;}
if (strlen($ldesc) > 195){
$desc = substr($ldesc,0,195).'...' ;
}else{$desc = $ldesc;}
?>
<div class='resultbox'>
<a class='normal' style='text-decoration:none;font-size:huge;font-weight:bold' href='<?php echo $link ?>'><?php echo $title; ?></a>
<br>
<div style='padding-top:3px;padding-bottom:4px;width:580px;'>
<font style='text-decoration:none;font-size:small;font-family:Arial;'>
<?php echo $desc; ?><br>
</font>
</div>
<a style='text-decoration:none;' href='<?php echo $link; ?>'><font style='text- decoration:none;font-size:small;color:green;font-weight:bold;'><?php echo $url; ?><br></font> </a>
<?php endforeach; ?>
That way you're not playing with big echos.
Now for generating random colors your could use php rand();
For example:
//Generate a random number between the two parameters
$randomNumber = rand(1, 3);
//Use this number to dictate what the variable color should be
if($randomNumber == 1){$color = "#333"}
elseif($randomNumber == 2){$color = "#555"}
elseif($randomNumber == 3){$color = "#999"}
You can then use the variable $color in your code to randomly assign one of the colors to elements.
Hope this helps!
-Gui

Categories