from the following code i want to take just a few value data from "elemen_desc" array. How to do it? I try to put if else statement in the option but the syntax is error. Maybe anyone can help? . Below is the following code.
public function ListElementDesc() {
$document = new Document_Template_Model();
$result = $document->ListElementDesc();
$html = '';
foreach ($result as $multi):
$html .= '<option value="' . $multi['element_desc'] . '"></option>'; //' . $multi['element_code'] . '
endforeach;
return $html;
}
It would be useful to see the syntax you did try that raised the error, however, perhaps this will help:
foreach ($result as $multi):
if (isset($multi['element_desc'][1])):
$html .= '<option value="' . $multi['element_desc'][1] . '"></option>'; //' . $multi['element_code'] . '
else:
$html .= '<option value="' . $multi['element_code'][0] . '"></option>'; //' . $multi['element_code'] . '
endif;
endforeach;
For further reference perhaps this page will help, PHP Alternative Syntax for Control Structures
Related
I am having problem in fetching data from database table, after loading data into array and loop using foreach it return blank instead of the values. I have tried using this way $row[''] it works fine but i want to use this operand -> but its fetching bank data.
$query = "SELECT * FROM pages_words";
$select_posts = mysqli_query($connection, $query);
$rows[] = array();
while ($row = mysqli_fetch_assoc($select_posts))
$rows[] = $row;
foreach ($rows as $key => $row) {
$idpw = $row['idpw'];
$pages = $row['pages'];
$words = $row['words'];
$amount_added = $row['amount_added'];
$page_type = $row['page_type'];
if ($page_type == 'double') {
if ($row->idpw == $post['no_of_pages']) {
echo '<option selected="selected" id="' . $row->idpw . 'nop" value="' . $row->idpw . '" title="' . $row->amount_added . '"> ' . $row->pages . ' Page(s) / ' . $row->words . ' Words</option>';
} else {
echo '<option id="' . $row->idpw . 'nop" value="' . $row->idpw . '" title="' .
$row->amount_added . '"> ' . $row->pages . ' Page(s) / ' . $row->words . ' Words</option>';
}
}
}
?>
blank data
kindly help solve the problem
You used mysqli_fetch_assoc(), which fetches rows as associative arrays. In other places in the foreach loop, you're accessing the row correctly using array syntax, such as $idpw = $row['idpw'];. You can't access array values using object syntax. Instead of using $row->idpw when outputting the option, you could either use $row['idpw'] or just use the $idpw variable you created earlier.
If you want the arrow operator to work, you'll need to use mysqli_fetch_object() instead, but if you do that you'll need to change the places where you're using array syntax to use object syntax as well.
I have two different method which help to make drop_down (select) option. Both are work same. only different is in logic for showing selected option
Method 1:
function unit_type_drop_down($selected=''){
$where = array('status'=>'1');
$unit_type_data = $this->select_result('unit_type', 'id,name', $where);
$html = '';
foreach ($unit_type_data as $value) {
if($value->id == $selected){
$html.='<option value="' . $value->id . '" selected>' . ucfirst($value->name) . '</option>';
}else{
$html.='<option value="' . $value->id . '">' . ucfirst($value->name) . '</option>';
}
}
return $html;
}
Method 2:
function unit_type_drop_down($selected=''){
$where = array('status'=>'1');
$unit_type_data = $this->select_result('unit_type', 'id,name', $where);
$html = '';
foreach ($unit_type_data as $value) {
$html.='<option value="' . $value->id . '">' . ucfirst($value->name) . '</option>';
}
$html = str_replace('value="'.$selected.'"', 'value="'.$selected.'" selected="selected"', $html);
return $html;
}
unit type data has value something like
$unit_type_data = array(
array('id'=>1,'name'=>'unit1'),
array('id'=>1,'name'=>'unit1'),
array('id'=>1,'name'=>'unit1'),
.
.
.
.
.
array('id'=>1,'name'=>'unit1')
);
The first method is faster because IF-ELSE statement and string concatenation consume fewer resources and operations than search and replacement in a string.
Of course, if you want to get exact metrics of speed both methods - should calculate asymptotic complexity for concatenation of strings and replacement string in PHP.
Also, the first method seems more clear to readable than the second method
You can use getusage to check execution time of any PHP script. Or with microtime before and after each script, like this:
$before = microtime(true);
// some script
$after = microtime(true);
echo ($after-$before);
I'm getting category from my database using PHP and I want to use it inside Google Analytics event tracking code. The problem is that events are not being recorded in Google Analytics.
Here are some code snippets from my project:
1)
$docs = array();
while ($row = mysql_fetch_assoc($result)) {
$doc = array();
$doc['my_tel'] = $row['my_tel'];
$doc['my_category'] = $row['my_category'];
$docs[] = $doc;
}
mysql_free_result($result);
2)
<?php
$html_output = '';
foreach ($docs as &$d) {
$html_output .= '<div>' .
'<img src="call.png" width="65px" height="35px" border="0">' .
'</div>';
}
echo $html_output;
?>
If you have a look at the console of your browser there might be a JavaScript error, because the second parameter of _gaq.push is not wrapped with quotes.
Try this:
<?php
$html_output = '';
foreach ($docs as &$d) {
$html_output .= '<div>' .
'<img src="call.png" width="65px" height="35px" border="0">' .
'</div>';
}
echo $html_output;
?>
Looking at your append to $html_output, it appears you're not enclosing the category within single quotes in the resultant javascript output.
Assuming for example my_tel was 01234567890 and category was 'Category 1' the output from the code above would be:
<div><img src="call.png" width="65px" height="35px" border="0"></div>
Perhaps try (note additional escaped quotes around category:
'<a href="tel:' . $d['my_tel'] . '" onClick="_gaq.push([\'_trackEvent\', \'' . $d['my_category'] . '\', \'Event Action\',...
--dan
I'm still a PHP noob, so I apologize if this is something simple.
I am creating a fairly basic search facility for a website using PHP and mySQL. I have connected to the database, selected the database, queried the table and have fetched the table columns;
$k = htmlspecialchars($_GET['k']); // Get search query
$select = mssql_query("SELECT * FROM search WHERE Title Like '%" . $k . "%'");
if( mssql_num_rows($select) < 1) {
$noResults = 'No results found for <b>' . $k . '</b>, <label for="k">Please try again.</label>';
} else {
while ($results = mssql_fetch_array($select)) {
$title = $results['Title'];
$link = $results['Link'];
$description = $results['Description'];
}
}
When I put the $results[''] columns into variables and then try to echo out each variable like so;
if( isset($noResults)) {
echo $noResults;
} else {
echo '<li>' . '<h2>' . '' . $title . '' . '</h2>' . '<p>' . $link . '</p>' . '<p>' . $description . '</p>' . '</li>';
}
it only echo's out one row matching that query however, If I was to just simple echo out the columns like so;
echo $results['Title'];
echo $results['Link'];
echo $results['Description'];
all rows matching the query will be displayed..
I'm not sure why this is happening. If someone could help me out that would be great!
You need to use a loop:
$k = mysql_real_escape_string($_GET['k']); // Get search query
$select = mssql_query("SELECT * FROM search WHERE Title Like '%" . $k . "%'");
if( mssql_num_rows($select) < 1) {
$noResults = 'No results found for <b>' . $k . '</b>, <label for="k">Please try again.</label>';
} else {
$results= array();
while ($result = mssql_fetch_array($select)) {
$results[]= $result;
}
}
if( isset($noResults)) {
echo $noResults;
} else {
echo "<ul>";
foreach($results as $result){
echo '<li>' . '<h2>' . '' . $result['title'] . '' . '</h2>' . '<p>' . $result['link'] . '</p>' . '<p>' . $result['description'] . '</p>' . '</li>';
}
echo "</ul>";
}
Do you execute the output in the while-loop?
If you execute the while-loop and call the echo after that, each resultset will overwrite the previous, and the echo will output the last resultset which was fetched.
If you call the echo in the Loop, every result set will generate "his own" output line.
If you want to hold every resultset in a variable you can use an array, which is declared in front of the loop and gets filled in the loop.
a few things are not clear from your question, but i am assuming that you are echo'ing the variables outside the loop since you are checking isset($noResults). that means you are reassigning the variables with new values in each loop of while. so ultimately you get the last one assigned to the variables. you have to either use an array to hold the values or echo it with in the loop.
I am really new to PHP programming, and I'm frustrated because I think this should work and it doesn't. I'm really missing something...
<?php
foreach ($datas as $name)
{
if ($name['state'] === 'MA')
{
echo
'<input type="hidden" name="id" value="' . $name['id'] . '" />' .
'<h2>' . htmlentities($name['name']) . '</h2>' .
'<p>' .
htmlentities($name['description']) . ' ' .
...
'<h1>' . $name['id'] . '</h1>';
foreach ($commentdatas as $name2)
{
if ($name['id'] == $name2['parkid'])
{
echo $name['id'] . $name2['parkid'];
}
}
}
}
?>
</div>
Everything worked well until I got to the second foreach statement. The foreach works. When I test, $name['id'] echos properly, as does $name2['parkid'].
There doesn't seem to be a problem with the system correctly identifying these, even in the loop. But inside the if statement, nothing echos at all.
Obviously something is wrong with the if statement. I've looked everywhere I can find and I'm having trouble figuring out the proper way to compare these variables. Can anyone help?
Try in_array() function
foreach ($commentdatas as $name2) {
if ($name['id'] == $name2['parkid'])
{
echo $name['id'] . $name2['parkid'];
}
}
}
replace with
if(in_array($name['id'],$commentdatas)){
echo $name['id'] . $name2['parkid'];
}