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>
Related
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..
I found this tutorial on making a shopping cart app and It's not working. I get an error about undefined variable bookFound on line 22 where it says if(!$bookFound). I see maybe why it is not defined, I'm thinking maybe because it was defined in the if statement previously in the code and that is not returning true. Any ways I'm having problems fixing it so if you can make this code work that will be great. The user should be able to click the button and the div should be updated with calculated results.
<?php
session_start();
$booksInfo = $_SESSION['cart'];
if(count($booksInfo) > 0)
{
$bookFound = false;
for($i=0; $i< count($booksInfo); $i++)
{
if($booksInfo[$i]['bookId'] == $_POST['bookId'])
{
$booksInfo[$i]['quantity'] = $_POST['quantity'];
$bookFound = true;
break;
}
}
}
if(!$bookFound) //line 22 where error was found
{
$book = array('bookId' => $_POST['bookId'], 'quantity' => $_POST['quantity']);
array_push($booksInfo, $book);
}
$_SESSION['cart'] = $booksInfo;
$grossTotal = 0;
for($i=0; $i< count($booksInfo); $i++)
{
$aBook = $booksInfo[$i];
$bookName = getBookName($booksInfo[$i]['bookId']);
$bookPrice = getPriceForBook($booksInfo[$i]['bookId']);
$totalPrice = $bookPrice * $booksInfo[$i]['quantity'];
$grossTotal+= $totalPrice;
$str.= '<strong>Name - </strong>'.$bookName;
$str.= '<br/>';
$str.= ' <strong>Copies - </strong>'.$booksInfo[$i]['quantity'];
$str.= '<br/>';
$str.= '<strong>Price - </strong>$'.$bookPrice. ' * ' .$booksInfo[$i]['quantity'].' = $'.$totalPrice;
$str.= '<br/><br/>';
}
$str.= '<strong>Net Amount - </strong>$'.$grossTotal;
echo $str;
function getBookName($id)
{
$objXML = simplexml_load_file('books.xml');
foreach($objXML->book as $book)
{
if($book['id'] == $id)
{
return $book->name;
}
}
return false;
}
function getPriceForBook($id)
{
$objXML = simplexml_load_file('books.xml');
foreach($objXML->book as $book)
{
if($book['id'] == $id)
{
return $book->price;
}
}
return false;
}
?>
Index:
<body>
<div class="cart">
<strong>Your cart</strong>
<p id = "cart">Cart is empty</p>
</div>
<?php
$objXML = simplexml_load_file('books.xml');
foreach($objXML->book as $book)
{
echo '<div>';
echo 'Name - ' . $book->name, '<br />';
echo 'price - $'. $book->price, '<br/>';
?>
Quantity -
<select name="" id="">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
-
<input type="hidden" value = "<?php echo $book['id']; ?>">
<input type="button" value = "Select this book ">
<?php
echo '</div>';
}
?>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('input:button').click(function(){
$.post('calculate.php',
{
bookId : $(this).prev('input:hidden').val(),
quantity: $(this).prev().prev('select').val()
},
function(data)
{
$('#cart').html(data);
}
)
});
});
</script>
</body>
Try moving the $bookFound = false; statement above the if(count($booksInfo) > 0) statement. If the if(count($booksInfo) > 0){} block is not executed, $bookFound is not initialized for when you get to the if(!$bookFound){} block.
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 />';
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 ...
};
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