how to loop the data retrieve from mysql using codeigniter - php

I have a problem wherein i will retrieve several datas in my sql, for example i have 9 datas but my problem is that inside my foreach the data that will be process and the foreach is placed in my controller.
here's my controller:
function getPhaseData() {
try
{
$this->SessionCheck();
$this->user->initialize($this->session->userdata('userid'));
$this->load->model('project_model', 'Project');
$ProjectID = $this->input->post('ProjectID');
/***************** Intialize Project model ******************/
$this->Project->Initialize($ProjectID);
$Options = Work_breakdown_structure::$WithBaseTaskID;
//$PhaseTaskID = (int)$this->input->get_post('TaskID',TRUE);
//$PhaseTaskID = $this->Project->getPhaseBaseTaskID($ProjectID);
$postlist->phaseList = $this->Project->LatestApplicablePlan->WBS->GetPhaseList($Options);
$PhaseList = $this->Project->LatestApplicablePlan->WBS->GetPhaseList($Options);
foreach($PhaseList as $row)
{
$postlist->taskList = $this->Project->LatestApplicablePlan->WBS->GetWBS($Iterate['TaskID'], $Options);
}
/*if($PhaseTaskID == null)
{ }
else
{
foreach($PhaseTaskID as $index=>$value)
{
$finalArr[$value['baseTaskID']] = $value['baseTaskID'];
//echo $finalArr[$value['baseTaskID']].' ';
$postlist->taskList = $this->Project->LatestApplicablePlan->WBS->GetWBS($finalArr[$value['baseTaskID']], $Options);
}
}*/
$postlist->project = $ProjectID;
return $this->load->view('MyToDoPhaseDropdown', $postlist);
}
here's my view where i will display the data get from the controller in my select dropdown.
if($project == 0) {
echo '<td style="padding-top:5x;font-size:14px;" colspan="2"> <br> Phases : ';
echo '<select disabled id="phases_select" style="width:400px;" onchange="search_filter()" >';
echo '<option value="0" selected="selected"> Select Project Phase </option>';
echo '</select>';
echo '</td>';
}
else
{
foreach($taskList as $iterate)
{
echo ' TaskID: '. ' '.$iterate['TaskID'] .' -- TaskName: '. $iterate['TaskName'].'<br>' ;
}
/*foreach($phaseList as $row) {
echo 'TaskID: '. $row['TaskID'].' '.$row['TaskName'].'<br>';
if(preg_match("/^CYCLE/", strtoupper($row['TaskName'])))
{
foreach($row['Child'] as $child) {
echo 'TaskID: '. $child['TaskID'].' '.$child['TaskName'].'<br>';
}
}
}*/
echo '<td style="padding-top:5x;font-size:14px;" colspan="2"> <br> Phases : ';
echo '<select id="phases_select" style="width:400px;" onchange="search_filter()" >';
echo '<option value="0" selected="selected"> Select Project Phase </option>';
foreach($phaseList as $row)
{
if(preg_match("/^CYCLE/", strtoupper($row['TaskName'])))
{
foreach($row['Child'] as $child)
{
if($Iterate['BaseTaskID'] != $child['TaskID'])
{
echo '<option value="'. $child['TaskID']. '">';
echo $row['TaskName'].' > '.$child['TaskName']. '</option>';
}
foreach($taskList as $Iterate)
{
if($child['TaskID'] == $Iterate['BaseTaskID'])
{
echo '<option value="'. $Iterate['TaskID']. '">';
echo $row['TaskName'].' > '.$child['TaskName'].' '.$Iterate['IterationNumber']. '</option>';
}
}
}
}
else
{
if($Iterate['BaseTaskID'] != $row['TaskID'])
{
echo '<option value="'. $row['TaskID']. '">';
echo $row['TaskName'].'</option>';
}
foreach($taskList as $Iterate)
{
if($row['TaskID'] == $Iterate['BaseTaskID'] )
{
echo '<option value="'. $Iterate['TaskID']. '">';
echo $row['TaskName'].' '.$Iterate['IterationNumber']. '</option>';
}
}
}
}
echo '</select>';
echo '</td>';
}

This is not on exact answer but this will help to understand MVC structure in codeigniter.
model
function get_city_list(){
$this->db->select('city_id, name');
$this->db->where('status', 1);
$this->db->order_by("name", "asc");
$this->db->from('city');
$query = $this->db->get();
$result = $query->result();
return $result;
}
Controller
function listing(){
$data['city'] = $this->home->get_city_list();
$this->load->view('property_listing', $data);
}
view
foreach($city as $cty){
echo $cty->name;
echo $cty->city_id;
}
Hope this helps you..

Related

Set the selected value in select2 multiple using PHP

How will i set the selected item in select2 multiple ? i would like to set these option/values as selected by default in a multiple select list and display them to the user where they will be able to updated their data if necessary.
<?php
$mytitle = array(
"867310020292434, 867310021548131, 867310021561670");
$test = implode(',', $mytitle);
$query=mysqli_query($link,"select * from inventory where ITEM_CODE_MX='OPP01-3006GRY'");
while($row=mysqli_fetch_array($query))
{
$imei=$row["IMEI_MX"];
$title = explode(',', $imei);
}
?>
<script>
$(function () {
$('#tags').select2();
function select (event){
$("#tags").select2({
maximumSelectionLength: $('#quantitytotransfer').val(),
formatSelectionTooBig: function (limit) {
$('#box').show().text('Callback!');
parseInt($("#quantitytotransfer").val())
return 'Too many selected elements (' + limit + ')';
}
});
}
$('#quantitytotransfer').on('keyup', select);
select()
});
</script>
<select id="tags" name='title[]' style="width:300px;" class="form-control select2-offscreen" onchange="getCount()" multiple>
<?php
foreach ($title as $opt) {
$sel = '';
$wew = trim($is);
if (in_array($opt, $mytitle)) {
$sel = 'selected="selected" ';
}
if (!empty($opt)) {
echo "<option ' . $sel . ' value='$opt'>$opt</option>";
}
}
?>
</select>
if you want to select the first option here is the code
$sel = "selected";
foreach ($title as $opt) {
if (!empty($opt)) {
echo "<option value='" . $opt . "'" . $sel . ">" . $opt . "</option>";
$sel = "";
}

Codeigniter getting multiple data in foreach in controller

I have a problem regarding my foreach loop wherein i cannot access the first array or the array[0] and i dont know the problem.
here is the controller:
$this->SessionCheck();
$this->user->initialize($this->session->userdata('userid'));
$this->load->model('project_model', 'Project');
$ProjectID = $this->input->post('ProjectID');
/***************** Intialize Project model ******************/
$this->Project->Initialize($ProjectID);
$Options = Work_breakdown_structure::$WithBaseTaskID;
//$PhaseTaskID = (int)$this->input->get_post('TaskID',TRUE);
$PhaseTaskID = $this->Project->getPhaseBaseTaskID($ProjectID);
$postlist->PhaseTaskID = $this->Project->getPhaseBaseTaskID($ProjectID);
$postlist->phaseList = $this->Project->LatestApplicablePlan->WBS->GetPhaseList($Options);
if($PhaseTaskID == null)
{ }
else
{
foreach($PhaseTaskID as $index=>$value)
{
$finalArr[$value['TaskName']] = $value['BasetaskID'];
$postlist->taskList = $this->Project->LatestApplicablePlan->WBS->GetWBS($finalArr[$value['TaskName']], $Options);
}
echo print_r($finalArr);
for($x = 1 ; $x < 2 ; $x++)
{
//$postlist->taskList = $this->Project->LatestApplicablePlan->WBS->GetWBS($phaseID, $Options);
}
}
$postlist->project = $ProjectID;
return $this->load->view('MyToDoPhaseDropdown', $postlist);
here is my view:
echo '<td style="padding-top:5x;font-size:14px;" colspan="2"> <br> Phases : ';
echo '<select id="phases_select" style="width:400px;" onchange="search_filter()" >';
echo '<option value="0" selected="selected"> Select Project Phase </option>';
foreach($phaseList as $row)
{
if(preg_match("/^CYCLE/", strtoupper($row['TaskName'])))
{
foreach($row['Child'] as $child)
{
echo '<option value="'. $child['TaskID']. '">';
echo $row['TaskName'].' > '.$child['TaskName'] . '</option>';
}
}
else
{
if($Iterate['BaseTaskID'] != $row['TaskID'])
{
echo '<option value="'. $row['TaskID']. '">';
echo $row['TaskName'].'</option>';
}
foreach($taskList as $Iterate)
{
if($row['TaskID'] == $Iterate['BaseTaskID'] )
{
echo '<option value="'. $row['TaskID']. '">';
echo $Iterate['TaskName'].' '.$Iterate['IterationNumber']. '</option>';
}
}
}
}
echo '</select>';
echo '</td>';
The problem is that i need to get all the values into an array to pass it to the view. but i only get the latest value which is the 2nd data that i retrieve in the database.
What you are doing wrong is :
return $this->load->view('MyToDoPhaseDropdown', $postlist);
You need to set the data in a variable to be accessible in view : http://codeigniter.com/user_guide/general/views.html
$data = array('title' => 'My Title',
'heading' => 'My Heading',
'message' => 'My Message');
$this->load->view('MyToDoPhaseDropdown', $data);
And in view file : MyToDoPhaseDropdown.php
<html>
<?php
//Access them like so
echo $title.$heading.$message; ?>
</html>

Get data from DB to set as default in dropdown HTML/CSS/MYSQL/PHP

Hello guys I want to get data from the DB to set as default/selected in my dropdown.
I have a page named Update Project, in that page I have a dropdown component that is filled by records from my data, however I want to set the selected value from the query result.
This is what I've tried so far:
$st_ac = $conn->prepare( "SELECT p.project_code, m.type
FROM tblprojects as p
JOIN tblprojectsmaster as m
ON p.project_code = m.project_code
WHERE p.project_code = :code" );
$st_ac->execute(array(':code' => $code));
$result = $st_ac->fetch(PDO::FETCH_ASSOC);
$ptype = $result['type']; // the data to be selected in the dropdwon
<select name="projectType">
<?php
for($i=0; $row_pt = $st_pt->fetch(); $i++){
$type = $row_pt['type'];
$description = $row_pt['description'];
echo '<option value"' . $type . '"';
if($ptype == $type)
echo 'selected="selected"';
echo '>' . $description . '</option>';
echo '<br />';
?>
<option value="<?php echo $type; ?>"><?php echo $description; ?></option>
<?php
}
?>
But I can't be able to achieve what I want. Any ideas? Your help will be truly appreciated. Thanks.
Try this
<select name="projectType">
<?php
for($i=0; $row_pt = $st_pt->fetch(); $i++){
$type = $row_pt['type'];
$description = $row_pt['description'];
echo '<option value="'.$type.'"';
if($ptype == $type)
{
echo 'selected="selected"';
}
echo '>'.$description.'</option>';
}
?>
May be you can use like this
echo '<option value="' . $type . '"'; if($ptype == $type) { echo 'selected="selected" } >'; echo $description . '</option>'; echo '<br />';

php- foreach statement duplicating results

I have a while and foreach loop that I want to use to print all of my results from a MySQL query. Below is my code for the while statement and the foreach statements.
WHILE:
while ($row_questions = mysql_fetch_array($result_questions)) {
$step_number[] = $row_questions['step_number'];
if ($row_questions['step_number_sub'] != 0) {
$step_number_sub[] = $row_questions['step_number_sub'];
} else {
$step_number_sub[] = "0";
};
if ($row_questions['step_required'] != 0) {
$step_required[] = "*";
} else {
$step_required[] = "";
};
$step_description[] = $row_questions['step_description'];
$step_outcome[] = $row_questions['step_outcome'];
$step_equipment[] = $row_questions['step_equipment'];
$test_changes[] = $row_questions['test_changes'];
};
and my FOREACH:
foreach ($step_number as $i => $step){
$even_odd = ( '-odd' != $even_odd ) ? '-odd' : '';
echo '<section class="zebra'.$even_odd.'">';
echo '<div class="span-2 number"><strong>'.$step_number[$i].'</strong></div>';
echo '<div class="span-20 description">'.$step_description[$i].'</div>';
echo '<div class="span-2">'.$step_required[$i].'</div>';
if ($step_outcome[$i] != null) {
echo '<div class="span-22 outcome"><strong>Desired Outcome</strong><br>'.$step_outcome[$i].'</div>';
}
echo '<div class="clear"></div>';
echo '<article class="results">';
echo '<div class="span-10">Did this step match the desired outcome?</div>';
echo '<div class="span-10">Notes:</div>';
echo '<div class="span-10">
<select name="question_'.$step[$i].'" id="question_'.$step[$i].'" required aria-required="true">
<option name="pass" value="Pass">Yes, this step was completed successfully</option>
<option name="fail" value="Fail">No, this step failed to complete. See notes below</option>
</select>
</div>';
echo '<div class="span-10"><textarea name="question_'.$step[$i].'" id="question_'.$step[$i].'" class="nots"></textarea></div>';
echo '</article>';
echo '<div class="clear"></div>';
echo '</section>';
};
I am getting multiple versions of the same results (4). Thanks in advance for any help and let me know if you need any other info!
Don't use multiple arrays. Use a class structure or array for each element. A simple way to do this:
while ($row_questions = mysql_fetch_array($result_questions))
questions[] = $row_questions;
foreach( $questions as $question )
{
$even_odd = ( '-odd' != $even_odd ) ? '-odd' : '';
echo '<section class="zebra'.$even_odd.'">';
echo '<div class="span-2 number"><strong>'.$question['step_number'].'</strong></div>';
echo '<div class="span-20 description">'.$question['step_description'].'</div>';
echo '<div class="span-2">'.$question['step_required'].'</div>';
if ($question['step_outcome'] != null)
{
echo '<div class="span-22 outcome"><strong>Desired Outcome</strong><br>'.$question['step_outcome'].'</div>';
}
... you get the drift ...
};

How to set multiple select options by default from array

I'm trying to retrieve options/values from my database in the from of an array i would like to set these option/values as selected by default in a multiple select list and display them to the user where they will be able to updated their data if necessary.
//data in database
$mytitle = array(
'Arbitrator',
'Attorney',
'Student',
'Other'
);
//data for multiple select
$title = array(
'Judge' ,
'Magistrate' ,
'Attorney' ,
'Arbitrator',
'Title Examiner' ,
'Law Clerk','Paralegal' ,
'Intern' ,
'Legal Assistant',
'Judicial Assistant',
'Law Librarian' ,
'Law Educator' ,
'Attorney',
'Student',
'Other'
);
echo "<select name='title[]' multiple='multiple'>";
$test = implode(',', $mytitle);
for ($i=0; $i<=14; $i++) {
if($test == $title[$i]) {
echo "<option selected value='$title[$i]'>$title[$i]</option>";
}
else {
echo "<option value='$title[$i]'>$title[$i]</option>";
}
}
echo "</select>";
I think you may have a logic error. Try this as your loop:
foreach ($title as $opt) {
$sel = '';
if (in_array($opt, $mytitle)) {
$sel = ' selected="selected" ';
}
echo '<option ' . $sel . ' value="' . $opt . '">' . $opt . '</option>';
}
Use the in_array() function.
for ($i=0; $i<=14; $i++) {
if(in_array($title[$i], $mytitle)){
echo "<option selected value='$title[$i]'>$title[$i]</option>";
}else {
echo "<option value='$title[$i]'>$title[$i]</option>";
}
}
Very simple with the help of jQuery where the select has the id test
$('#test option').attr('selected', 'selected');
JSFiddle Example

Categories