Embedded youtube videos are cutting my messages from chat - php

I am with some problems in my php script when I try to envoke embedded youtube videos when reply, or send a message.
My php script is:
<?php
if($_GET['u'] != $user) {
$messages = addslashes($_POST['message']);
$messages = preg_replace_callback('#(?:https?://\S+)|(?:www.\S+)|(?:\S+\.\S+)#', function($arr)
{
if(strpos($arr[0], 'http') !== 0)
{
$arr[0] = 'http://' . $arr[0];
}
$url = parse_url($arr[0]);
// images
if(preg_match('#\.(png|jpg|gif)$#', $url['path']))
{
echo '<img src="'. $arr[0] . '" />';
}
// youtube
if(in_array($url['host'], array('www.youtube.com', 'youtube.com'))
&& $url['path'] == '/watch'
&& isset($url['query']))
{
parse_str($url['query'], $query);
return sprintf('<iframe width="560" height="315" src="http://www.youtube.com/embed/%s" frameborder="0" allowfullscreen></iframe>', $query['v']);
}
//links
return nl2br(sprintf('%1$s ', $arr[0]));
}, $messages);
$to = $_GET['u'];
$selPictureMsg = "SELECT * FROM users WHERE username = '$user'";
$result = mysqli_query($sql, $selPictureMsg);
$msgPicRow = mysqli_fetch_assoc($result);
$selPictureMsg11 = "SELECT * FROM users WHERE username = '$to'";
$result11 = mysqli_query($sql, $selPictureMsg11);
$msgPicRow11 = mysqli_fetch_assoc($result11);
$user_pic = $msgPicRow['profile'];
$receiver_pic = $msgPicRow11['profile'];
if(isset($_POST['msgSend'])) {
if(strlen($messages) <= 1) {
echo "<div class='error'>Sorry! Your message is too small. It needs to be more than 1 character.</div>";
} else {
$insMsg = "INSERT INTO messages VALUES('','$user','$to','$messages','0','$user_pic','$receiver_pic', NOW())";
$sql->query($insMsg);
echo "<div class='success'>Your message has been sent to ".$to." successfully.</div><br />";
}
}
$selmsg1 = "SELECT * FROM messages WHERE (to_user='$user' AND from_user='$to') OR (to_user ='$to' AND from_user='$user') ORDER BY date DESC";
$resultmsg1 = $sql->query($selmsg1);
$rownum = mysqli_num_rows($resultmsg1);
if($rownum > 0) {
$i = '';
echo "<table>
<tr class='chat'>";
while($assocMsg = mysqli_fetch_assoc($resultmsg1)) {
if($user == $_GET['from'] || $user != $assocMsg['to_user']) {
$smilies = array(
':|' => 'bored',
':-|' => 'bored',
':-o' => 'scared',
':-O' => 'scared',
':o' => 'scared',
':O' => 'scared',
';)' => 'wink',
';-)' => 'wink',
':p' => 'tongue',
':-p' => 'tongue',
':P' => 'tongue',
':-P' => 'tongue',
':D' => 'happy',
':-D' => 'happy',
';P' => 'wink-tongue',
';-P' => 'wink-tongue',
':)' => 'smile',
':-)' => 'smile',
':(' => 'sad',
':-(' => 'sad',
'<3' => 'heart',
':*' => 'kiss',
'*_*' => 'in-love',
';(' => 'crying',
':a' => 'angry',
':#' => 'rage',
':3' => 'normal-kiss',
'><' => 'shy',
';D' => 'happily',
';-D' => 'happily',
':$' => 'blushing',
'o_o"' => 'dont-get',
'XD' => 'laugh',
'*lookdown*' => 'look-down',
'*lookup*' => 'look-up',
'*lookleft*' => 'look-left',
'*lookright*' => 'look-right',
'$_$' => 'money',
'$.$' => 'money',
'#_#' => 'nerd',
'#.#' => 'nerd',
'*vomit*' => 'vomit'
);
$replace = array();
foreach ($smilies as $smiley => $imgName)
{
array_push($replace, '<img src="images/emoticons/'.$imgName.'.png" alt="'.$smiley.'" />');
}
$assocMsg["message"] = str_replace(array_keys($smilies), $replace, $assocMsg["message"]);
echo "
<div style='width:100%; max-height: 300px; overflow: auto;'>
<td><a href='profile.php?u=".$assocMsg['from_user']."'><img src='".$assocMsg['user_picture']."' width='auto' height='70px' /></a></td>
<td>".$assocMsg['message']."<br />".$video."</td>
</tr>
<tr>
<td colspan='5'><i>".$assocMsg['date']."</i></td>
</div>
";
$i++;
if($i % 1 == 0) {
echo "</tr><tr class='chat'>";
}
} else {
echo "
<div style='width:100%; max-height: 300px; overflow: auto;'>
<td><a href='profile.php?u=".$assocMsg['to_user']."<img src='".$assocMsg['friend_picture']."' width='auto' height='70px' /></a></td>
<td>".$messages."</td>
</tr>
<tr>
<td colspan='5'><i>".$assocMsg['date']."</i></td>
</div>
";
$i++;
if($i % 1 == 0) {
echo "</tr><tr class='chat'>";
}
}
}
echo "</tr>
</table><hr />";
} else {
echo "You don't have any conversations with $to<br /><hr />";
}
?>
Now I will show you in images the result before and after embedded youtube videos.
Before:
Before embedded youtube video
After:
After embedded youtube video
I don't know why is cutting the other messages, which are stored in database, and only shows the video, without showing the other messages as well.
In here return sprintf('<iframe width="560" height="315" src="http://www.youtube.com/embed/%s" frameborder="0" allowfullscreen></iframe>', $query['v']); , I tried with echo sprintf(....) as well but with echo is not storing on database.
And if I let the return it shows what image After: is showing.
So, what I want is to the embedded youtube video, appears as long the text you send the video + showing the older messages from the chat.

Related

Pagination for a table contents in yii

I'm using Yii application. In that how to give pagination to a table contents (not using cgridview).
In view,
<table data-provides="rowlink" data-page-size="20" data-filter="#mailbox_search" class="table toggle-square default footable-loaded footable line-content" id="mailbox_table">
<thead>
<tr>
<th></th>
<th data-hide="phone,tablet">From</th>
<th>Subject</th>
<th data-hide="phone" class="footable-last-column">Date</th>
</tr>
</thead>
<tbody>
<?php
foreach ($model as $item) {
if ($item->fromuser->usertypeid === '0') {
$name = $item->fromuser->username;
} else if ($item->fromuser->usertypeid === '1') {
$student = Student::model()->findByPk($item->fromuser->userid);
$name = $student->student_admissionno . " - " . $student->student_firstname . " " . $student->student_middlename . " " . $student->student_lastname;
} else if ($item->fromuser->usertypeid === '3') {
$guardian = Guardian::model()->findByPk($item->fromuser->userid);
$name = $guardian->guardian_name;
} else {
$employee = Employeemaster::model()->findByPk($item->fromuser->userid);
$name = $employee->employee_code . " - " . $employee->employee_firstname . " " . $employee->employee_middlename . " " . $employee->employee_lastname;
}
if ($item->email_status == 1)
echo '<tr id="' . $item->emailid . '" class="unreaded rowlink" style="display: table-row;">';
else
echo '<tr id="' . $item->emailid . '" class="rowlink" style="display: table-row;">';
echo '<td class="nolink footable-first-column">';
echo '<span class="footable-toggle"></span>';
echo '<input type="checkbox" class="mbox_msg_select" name="msg_sel"></td>';
echo '</span></td>';
echo '<td>' . $name . '</td>';
echo '<td>' . $item->email_subject . '</td>';
$originalDate = $item->time;
$newDate = date("Y-M-d H:i:s", strtotime($originalDate));
echo '<td class="footable-last-column">' . $newDate . '</td></tr>';
}
?>
</tbody>
</table>
<div class="main-detail-con" style="width:700px;">
<div style="width:700px; padding-left: 0px;" class="listing-center-numbers">
<?php
$this->widget('CLinkPager', array(
'pages' => $pages,
'currentPage' => $pages->getCurrentPage(),
'itemCount' => $item_count,
'pageSize' => $page_size,
'maxButtonCount' => 5,
));
?>
</div>
</div>
<div class="clear"></div>
In Controller,
public function actionEmail() {
$criteria = new CDbCriteria;
$criteria->order = 'time DESC';
$criteria->condition = 'to_userid = :id';
$criteria->params = array(':id' => Yii::app()->user->id);
$criteria->addCondition('t.email_status= "2" OR
t.email_status= "1"');
$item_count = Email::model()->count($criteria);
$model = Email::model()->findAll($criteria);
$pages = new CPagination($item_count);
$pages->setPageSize(Yii::app()->params['listPerPage']);
$pages->applyLimit($criteria);
$this->render('inbox', array(
'model' => $model,
'item_count' => $item_count,
'page_size' => Yii::app()->params['listPerPage'],
'items_count' => $item_count,
'pages' => $pages,
));
}
In main/config.php
'params' => array(
'listPerPage'=> 2,//default pagination size
),
Output is,
The page size (I am giving 2 for per page) is not working by using this code. Please help me...
First of all define a new params in config/main.php, something like:
'params'=>array(
// this is used in contact page
'adminEmail'=>'webmaster#example.com',
'listPerPage'=> 10,//default pagination size
),
Now, modify your controller's action like:
public function actionOutbox() {
$criteria = new CDbCriteria;
$criteria->condition = 'from_userid = :id';
$criteria->order = 'time DESC';
$criteria->params = array (':id'=>Yii::app()->user->id);
$item_count = Email::model()->count($criteria);
$model = Email::model()->findAll($criteria);
$pages = new CPagination($item_count);
$pages->setPageSize(Yii::app()->params['listPerPage']);
$pages->applyLimit($criteria);
$this->render('outbox', array(
'model' => $model,
'item_count'=>$item_count,
'page_size'=>Yii::app()->params['listPerPage'],
'items_count'=>$item_count,
'pages'=>$pages,
));
}
Then in your view after foreach loop add CLinkPager widget, like:
<?php
$this->widget('CLinkPager', array(
'currentPage' => $pages->getCurrentPage(),
'itemCount' => $item_count,
'pageSize'=> $page_size,
'maxButtonCount' => 5,
//'nextPageLabel' =>'My text >',
'htmlOptions' => array('class'=>'pages'),
));
?>
Thats all you need to do, you might have to do some css changes though, hope that helps :)

PHP or JQUERY JSON Handling

I have a simple JSON StdClass Object from a PHP, and I wish to format it into a table/list/div and eliminate other keys and values in the process. The JSON looks like this:
stdClass Object (
[msc] => 150
[number] => 309
[status] => OK
[msc_mcc] => 652
[imsi] => 652010154107728
[mcc] => 652
[operator_country] => Botswana
[msc_operator_name] => MSC
[msc_operator_country] => Botswana
[msc_mnc] => 01
[mnc] => 01
[id] => 1072540715
[msc_location] =>
[operator_name] => MSC )
I have tried PHP and did make a table, but the problem is I need to pick certain values other that the whole body, and also I need to eliminate empty values
function print_nice($elem,$max_level=10,$print_nice_stack=array()){
if(is_array($elem) || is_object($elem)){
if(in_array($elem,$print_nice_stack,true)){
echo "<font color=red>RECURSION</font>";
return;
}
$print_nice_stack[]=&$elem;
if($max_level<1){
echo "<font color=red>nivel maximo alcanzado</font>";
return;
}
$max_level--;
echo "<table class='table table-bordered table-striped'>";
if(is_array($elem)){
echo '<tr><th colspan=2><strong><font><h3>Results, with love</h3></font></strong></th></tr>';
}else{
echo '<tr><th colspan=2 class=hdrs><strong>';
echo '<font color=white>OBJECT Type: '.get_class($elem).'</font></strong></th></tr>';
}
$color=0;
foreach($elem as $k => $v){
if($max_level%2){
$rgb=($color++%2)?"#f5f5f5":"#efeeee";
}else{
$rgb=($color++%2)?"#f5f5f5":"#efeeee";
}
echo '<tr><td valign="top" style="width:40px;background-color:'.$rgb.';">';
echo '<strong>'.$k."</strong></td><td>";
print_nice($v,$max_level,$print_nice_stack);
echo "</td></tr>";
}
echo "</table>";
return;
}
if($elem === null){
echo "<font color=green>NULL</font>";
}elseif($elem === 0){
echo "0";
}elseif($elem === true){
echo "<font color=green>TRUE</font>";
}elseif($elem === false){
echo "<font color=green>FALSE</font>";
}elseif($elem === ""){
echo "<font color=green>EMPTY STRING</font>";
}else{
echo str_replace("\n","<strong><font color=red>*</font></strong><br>\n",$elem);
}
}
get_object_vars() and in_array() may be helpful here
For example:
<?php
$object = json_decode($jsonstring);
?>
<table>
<?php
foreach (get_object_vars($object) as $k => $v)
{
if (in_array($k, array('msc', 'number', 'status')) && ! empty($v))
{
echo '<tr>';
echo "<td>{$k}</td><td>{$v}</td>";
echo '</tr>';
}
}
?>
</table>
Where $object is the name of your json_decoded variable
Edit:
Added a check for empty values too

'Undefined index' error when using foreach loop

I've 2 tables named preacher & Sermons
Fields of Preacher
preacher_id
first_name
last_name
preacher_image
preacher_logo
preacher_bio_brief
category
fields of sermons
sermon_id
preacher_id
sermon_title
sermon_image
audio_file
sermon_description
sort_order
I want to display all the sermons of each preacher by the order of preacher first_name.I got it properly but also got the below error
A PHP Error was encountered
Severity: Notice
Message: Undefined index: 1
Filename: home/sermons_view.php
Line Number: 27
method in controller
function index() {
$res = $this->sermon_model->viewAllpreachers();
$this->data['preachers'] = $res;
$this->data['page'] = $this->config->item('APP_template_dir') . 'site/home/ sermons_view';
$this->load->vars($this->data);
$this->load->view($this->_container);
}
Method in model
function viewAllpreachers() {
$preacher = array();
$this->db->select('*');
$this->db->from('preacher');
$this->db->order_by('first_name');
$query = $this->db->get();
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$preacher[$row->preacher_id]['preacher_id'] = $row->preacher_id;
$preacher[$row->preacher_id]['preacher_name'] = $row->first_name . ' ' . $row->last_name;
$preacher[$row->preacher_id]['preacher_image'] = $row->preacher_image;
$preacher[$row->preacher_id]['preacher_bio_brief'] = $row->preacher_bio_brief;
$this->db->select('*');
$this->db->from('sermons');
$this->db->where('preacher_id',$row->preacher_id);
$query = $this->db->get();
if ($query->num_rows() > 0) {
foreach ($query->result() as $row1) {
$preacher[$row1->preacher_id][$row1->sermon_id]['sermon_id'] = $row1->sermon_id;
$preacher[$row1->preacher_id][$row1->sermon_id]['preacher_id'] = $row1->preacher_id;
$preacher[$row1->preacher_id][$row1->sermon_id]['sermon_image'] = $row1->sermon_image;
$preacher[$row1->preacher_id][$row1->sermon_id]['sermon_title'] = $row1->sermon_title;
$preacher[$row1->preacher_id][$row1->sermon_id]['audio_file'] = $row1->audio_file;
$preacher[$row1->preacher_id][$row1->sermon_id]['sermon_description'] = $row1->sermon_description;
}
}
}
return $preacher;
}
return false;
}
code in View
<?php
if ($preachers) {
foreach ($preachers as $val) {
?>
<tr>
<td><img src="<?= base_url(); ?>uploads/<?= $val['preacher_image']; ?>"/></td>
<td colspan="2"><?= $val['preacher_name']; ?></td>
<td colspan="2"><?= $val['preacher_bio_brief']; ?></td>
</tr>
<?
echo '<pre>';
//print_r($val);
echo '</pre>';
foreach ($val as $val1) {
?>
<tr>
<td style="padding-left: 50px; "><img src="<?= base_url(); ?>uploads/<?= $val1['sermon_image']; ?>" style="width: 60px;height: 60px;"/></td>
<td><?= $val1['sermon_title']; ?></td>
<td><?= $val1['audio_file']; ?></td>
<td width="250px"><?php $res = explode('/', $val1['audio_file']);
if ($val1['audio_file']) {
?><a id="play" href="<?= site_url('sermons/playmp3/' . $val1['sermon_id']); ?>"><?= $res['1']; ?></a><?php } ?></td>
<td><?= $val1['audio_file']; ?></td>
</tr>
<?
}
}
}
?>
I think its the problem with the resultant array
I got that like
Array
(
[21] => Array
(
[preacher_id] => 21
[preacher_name] => Vance Havner
[preacher_image] => profile_image/havner.jpeg
[preacher_bio_brief] => this is for testing
[42] => Array
(
[sermon_id] => 42
[preacher_id] => 21
[sermon_image] => sermon_image/image_81322797345.jpg
[sermon_title] => 3 notes of the devil's tales
[audio_file] => audio_file/Niranja_Mizhiyum1.mp3
[sermon_description] => 3 notes of the devil's tales
)
[41] => Array
(
[sermon_id] => 41
[preacher_id] => 21
[sermon_image] =>
[sermon_title] => The Lordship of Christs
[audio_file] => audio_file/Naladhamayanthi_Kadhayile.mp3
[sermon_description] => the lordship of christ
)
)
)
there is no $res in your model
first debug what is $res with print_r($res) and
AFAIK there must be $val1['sermon_title'] instead of $res['1']

auto update of web page upon update of database

i am currently having some problems. i am creating a website with a chat feature on it. as of now, we were able to update the area wherein messages would appear upon user submits a message. but we are having some problems. we used jquery so that the whole page would not reload but instead only the iframe alotted for the chat only.the chat would work properly and after a few more minutes, it would start to reload over and over again. here is what we have right now...
<?php
session_start();
include "connect.php";
$room = $_SESSION['room'];
$getnummessages="SELECT COUNT(*) as messagecount from tbl_chatmessages";
$getnummessages2=mysql_query($getnummessages) or die("blah");
$getnummessages3=mysql_result($getnummessages2, 0);
if($getnummessages3>21)
{
$startrow=$getmessages3-20;
}
else
{
$startrow=1;
}
date_default_timezone_set ("Asia/Manila");
$date = date("Y-m-d");
// Configuration part
$path = "images"; // Path to the directory where the emoticons are
//smiley
// Query the database, and assign the result-set to $result
$query = "SELECT emote, image FROM emoticons";
$result = mysql_query($query);
// Loop through the results, and place the results in two arrays
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$emotes[] = $row['emote'];
$images[] = "<img src='" . $path . "/" . $row['image'] . "'>";
}
// The line below replaces the emotes with the images
echo str_replace($emotes, $images, $text);
$getmsg="SELECT * from tbl_chatmessages a, jcow_accounts b WHERE a.room_number='$room' && b.username=a.user_alias && a.date='$date' ORDER BY postime DESC";
$getmsg2=mysql_query($getmsg) or die(mysql_error());
while($getmsg3=mysql_fetch_array($getmsg2))
{
//$message=Smilify($subject); //Smiley faces
print "<table name='tablechat' id='tablechat' cellspacing='0' cellpadding='0' style='margin-top:0px;margin-left:0px;padding:0px;'>";
print "<tr><td rowspan='2'><a href='index.php?p=u/$getmsg3[user_alias]' target='_blank'>
<img src='http://www.pinoyarea.com/uploads/avatars/$getmsg3[avatar]' width='50px' height='50px'/></td><td><font color='#333333' style='text-decoration:none;font-size:14px;font-family:tahoma;'><b> $getmsg3[name]</b></font></a> <font color='#666666' style='text-decoration:none;font-size:10px;font-family:tahoma;'>($getmsg3[time]):</font></td></tr><tr><td><font style='font-family:tahoma;font-size:12px;padding-left:5px;'>".str_replace($emotes, $images, $getmsg3[message])."</font></td></tr>";
print "</table>";
}
function Smilify(&$subject)
{
$smilies = array(
':D' => 'icon_biggrin',
':)' => 'icon_smile',
':(' => 'icon_sad',
':o' => 'icon_surprised',
':shock:' => 'icon_eek',
':?' => 'icon_confused',
':8' => 'icon_cool',
':lol:' => 'icon_lol',
':x:' => 'icon_mad',
':p' => 'icon_razz',
':red:' => 'icon_redface',
':cry:' => 'icon_cry',
':evil:' => 'icon_evil',
':twisted:' => 'icon_twisted',
':roll:' => 'icon_rolleyes',
':wink:' => 'icon_wink',
':!:' => 'icon_exclaim',
':?:' => 'icon_question',
':idea:' => 'icon_idea',
':arrow:' => 'icon_arrow',
);
$sizes = array(
'icon_cry' => 18,
'icon_cool' => 20,
'haha' => 20,
'icon_surprised' => 20,
'icon_exclaim' => 20,
'icon_razz' => 20,
'icon_mad' => 18,
'icon_rolleyes' => 20,
'icon_wink' => 20,
);
$replace = array();
foreach ($smilies as $smiley => $imgName)
{
$size = $sizes[$imgName];
array_push($replace, '<img src="images/'.$imgName.'.gif" alt="'.$smiley.'" width="'.$size.'" height="'.$size.'" />');
}
$subject = str_replace(array_keys($smilies), $replace, $subject);
}
?>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"> </script>
<script>
$(document).ready(function() {
$("#tablechat").load("chatlog.php");
var refreshId = setInterval(function() {
$("#tablechat").load('chatlog.php?randval='+ Math.random());}, 6000);
});
</script>
Is that the code for the page that gets loaded into the iframe? If so, the problem is probably that you have the code for the timer inside it. Every time you reload the frame you're adding a new interval timer, each time that timer runs it loads a new timer and so on.
The solution is to move the JavaScript you have there at the end out of the iframe and put it in the parent page instead.
the best way i recommend CometD for always pull,it is adavenced technology for push
http://cometd.org/

How do insert and retrieve checkbox value using codeigniter?

My problem is, I key in the sibling details in the first row textboxs and i select the check box no error display. If i didn't select the checkbox its display the below error. Can you give some idea? i am new for CodeIgniter.
I got error message: (How i can solve the error)
A PHP Error was encountered
Severity: Notice
Message: Undefined offset: 1
Filename: controllers/registration.php
Line Number: 288
Below my Code:
foreach($querystud_frm->result() as $row)
{
echo "<tr><td>".$i.")</td>";
$data=array('name'=>'s_sibling_name[]','class'=>'textbox','value'=>$row->s_sibling_name,'style'=>'text-transform:uppercase;');
echo "<td >".form_input($data)."</td>";
$data=array('name'=>'s_sibling_nric[]','class'=>'textbox','value'=>$row->s_sibling_nric,'style'=>'text-transform:uppercase;');
echo "<td >".form_input($data)."</td>";
$sib_dob=date('d-m-Y',strtotime(trim($row->s_sibling_dob)));
$data=array('name'=>'s_sibling_dob[]','class'=>'textbox','value'=>$sib_dob,'style'=>'text-transform:uppercase;');
echo "<td >".form_input($data)."</td>";
$data=array('name'=>'s_sibling_relation[]','class'=>'textbox','value'=>$row->s_sibling_relation,'style'=>'text-transform:uppercase;');
echo "<td >".form_input($data)."</td>";
$data=array('name'=>'s_sibling_occupation[]','class'=>'textbox','value'=>$row->s_sibling_occupation,'style'=>'text-transform:uppercase;');
echo "<td >".form_input($data)."</td>";
$data=array('name'=>'s_sibling_income[]','class'=>'textbox','value'=>$row->s_sibling_income,'style'=>'text-transform:uppercase;');
echo "<td >".form_input($data)."</td>";
$sib_yes = $row->s_sibling_yes;
echo $sib_yes;
if($sib_yes == 'YES')
{
$data= array('name' => 's_sibling_yes[]', 'id' => 's_sibling_yes', 'value' => 'YES', 'checked' => TRUE);
echo "<td >".form_checkbox($data)."</td>";
}
else
{
$data= array('name' => 's_sibling_yes[]', 'id' => 's_sibling_yes', 'value' => 'NO', 'checked' => FALSE);
echo "<td >".form_checkbox($data)."</td>";
}
echo "</tr>";
$i++;
}
if($i<10)
{
$var=array("s_sibling_name","s_sibling_nric","s_sibling_dob","s_sibling_relation","s_sibling_occupation","s_sibling_income");
for($j=$i;$j<=10;$j++)
{
echo "<tr><td>".$j.")</td>";
foreach($var as $value)
{
$value=$value.'[]';
$data_val=array('name'=>$value,'class'=>'textbox','value'=>'','style'=>'text-transform:uppercase;');
echo "<td>".form_input($data_val)."</td>";
}
if($sib_yes == 'YES')
{
$data= array('name' => 's_sibling_yes[]', 'id' => 's_sibling_yes', 'value' => 'YES', 'checked' => TRUE);
echo "<td >".form_checkbox($data)."</td>";
}
else
{
$data= array('name' => 's_sibling_yes[]', 'id' => 's_sibling_yes', 'value' => 'NO', 'checked' => FALSE);
echo "<td >".form_checkbox($data)."</td>";
}
echo "</tr>";
}
unset($var);
}
$querystud_frm->free_result();
}
else
{
$var=array("s_sibling_name","s_sibling_nric","s_sibling_dob","s_sibling_relation","s_sibling_occupation","s_sibling_income");
$var_sib=array("s_sibling_yes");
for($i=1;$i<=10;$i++)
{
echo "<tr><td>".$i.")</td>";
foreach($var as $value)
{
$name=$value.'[]';
$data=array('name'=>$name,'class'=>'textbox','value'=>'','style'=>'text-transform:uppercase;');
echo "<td >".form_input($data)."</td>";
}
$data= array('name' => 's_sibling_yes[]', 'id' => 's_sibling_yes', 'value' => 'YES', 'checked' => FALSE);
echo "<td >".form_checkbox($data)."</td>";
echo "</tr>";
}
unset($var,$i,$data,$var_sib);
}
controller/registration.php
if($this->input->post('siblingsfrm'))
{
$this->data['nric']=$this->input->post('s_nric');
//echo $this->input->post('name');
$name=$this->input->post('s_sibling_name');
$nric=$this->input->post('s_sibling_nric');
$dob=$this->input->post('s_sibling_dob');
//$dob=strtotime($dob);
//$dob=date('Y-m-d',strtotime($dob));
$relation=$this->input->post('s_sibling_relation');
$occupation=$this->input->post('s_sibling_occupation');
$income=$this->input->post('s_sibling_income');
$sib_yes=$this->input->post('s_sibling_yes');
$sib_id=$this->input->post('s_sibling_id');
//print_r($sib_id);
//print_r($name);
//exit();
//$this->load->helper('date');
for($i=0;$i<count($name);$i++)
{
if($name[$i]!='')
{
$income[$i]=trim($income[$i]);
if($income[$i]=='')
$income[$i]=0;
if($sib_yes[$i]!= 'YES')
$sib_yes[$i]='NO';
$date=date('Y-m-d',strtotime(trim($dob[$i])));
$insert_values=array('s_nric'=>$this->data['nric'],'s_sibling_name'=>strtoupper(trim($name[$i])),
's_sibling_nric'=>strtoupper(trim($nric[$i])),'s_sibling_dob'=>$date,
's_sibling_relation'=>strtoupper(trim($relation[$i])),
's_sibling_occupation'=>strtoupper(trim($occupation[$i])),
's_sibling_income'=>strtoupper(trim($income[$i])),
's_sibling_yes'=>trim($sib_yes[$i]));
if(is_array($sib_id) && isset($sib_id[$i]) )
{
$where=array('s_sibling_id'=>$sib_id[$i]);
$this->db->update('si_student_siblings',$insert_values,$where);
}
else
{
//print_r($insert_values);
$this->db->insert('si_student_siblings',$insert_values);
}
}
}
$this->data['level']=7;
}
The problem is that if a checkbox wasn't checked, it won't send any data at all to the server. So, this line of code...
$name=$this->input->post('s_sibling_name');
...will actually set the $name variable to FALSE (see $this->input->post() documentation in the guide)
Thus, the first time through your loop, your code is looking for the array item at $name[0], when in fact $name is not even an array, it's FALSE.
You could surround your for loop with an if/else statement to solve this problem (not the most elegant solution, but it would work):
if (is_array($name) && count($name) > 0)
{
for($i=0;$i<count($name);$i++)
{
if($name[$i]!='')
//etcetera...
}
}

Categories