PHP Foreach with two queries - php

Ok i have one query in codeigniter where i select all data
function get_story()
{
$this->db->select('*');
$this->db->from('stories');
$query = $this->db->get();
return $query;
}
then i make another query with data from another table
function get_stories()
{
$this->db->from('stories');
$this->db->join('stories_to_categories', 'stories.id = stories_to_categories.story');
$this->db->join('categories', 'stories_to_categories.category = categories.id');
$query = $this->db->get();
return $query;
}
Then i am trying to create table with data from two queries. Making one query with another JOIN its not option. I created foreach
foreach($story->result() as $row)
{
echo '<tr>
<td>'.$row->title_english.'</td>
<td>'?????????????????????????????????????????????????????</td>
<td align="center">
<img src="'.site_url('admin/images/icons/pencil.png').'" class="tooltip" title="'.lang('edit').'" />
<img src="'.site_url('admin/images/icons/minus-circle.png').'" class="tooltip" title="'.lang('delete').'" />
</td>
</tr>
';
echo '<div id="delete_'.$row->id.'" style="display: none;">
<h1>'.lang('client_delete').'</h1><br />
'.lang('story_confirm_delete').'
<br /><br />
<span><span>'.lang('yes').'</span></span>
<span><span>'.lang('no').'</span></span>
<br /><br />
</div>';
}
In td tag where is ???? i need results from second query, i have no idea how to insert data from second query into foreach??? Does somebody have idea, helpe is really needed?

If I understand your question, I think you just need to use string concatenation. Prepare your HTML output into a variable and print it later.
$html = '<table>';
foreach($story->result() as $row)
{
$html .= '<tr>';
$html .= '<td>'.$row->title_english.'</td>';
$html .= '<td>';
foreach($stories->result() as $row2){
// display your second query result here
$html .= '?????????????????????????????????????????????????????';
}
$html .= '</td>';
$html .= '<td align="center">';
$html .= '<img src="'.site_url('admin/images/icons/pencil.png').'" class="tooltip" title="'.lang('edit').'" />';
$html .= '<img src="'.site_url('admin/images/icons/minus-circle.png').'" class="tooltip" title="'.lang('delete').'" />';
$html .= '</td>';
$html .= '</tr>';
$html .= '<div id="delete_'.$row->id.'" style="display: none;">';
$html .= '<h1>'.lang('client_delete').'</h1><br />';
$html .= lang('story_confirm_delete');
$html .= '<br /><br />';
$html .= '<span><span>'.lang('yes').'</span></span>';
$html .= '<span><span>'.lang('no').'</span></span>';
$html .= '<br /><br />';
$html .= '</div>';
}
$html .= '</table>';
echo $html;

Related

How to place foreach loop inside a <td> tag [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
How do you place a foreach loop inside a tag?
here's the code:
foreach( $designer_posts as $designer_post ) {
$tags = get_the_tags($designer_post->ID);
$imageThumb = get_the_post_thumbnail( $designer_post->ID, "thumbnail" );
$html .= '<tr>';
$html .= '<th scope="row">View</th>';
$html .= '<td><p>'.$designer_post->post_title.'</p></td>';
$html .= '<td>'FOREACH LOOP HERE!!!'</td>';
$html .= '<td>'.$imageThumb.'</td>';
$html .= '</tr>';
}
I keep getting an error. Could you help me with the PROPER syntax?
foreach( $designer_posts as $designer_post ) {
$tags = get_the_tags($designer_post->ID);
$imageThumb = get_the_post_thumbnail( $designer_post->ID, "thumbnail" );
$html .= '<tr>';
$html .= '<th scope="row">View</th>';
$html .= '<td><p>'.$designer_post->post_title.'</p></td>';
$html .= '<td>';
foreach() {
$html .= 'The rest of the HTML code';
}
$html .= '</td>';
$html .= '<td>'.$imageThumb.'</td>';
$html .= '</tr>';
}
I think you want to do this :
foreach( $designer_posts as $designer_post ) {
$tags = get_the_tags($designer_post->ID);
$imageThumb = get_the_post_thumbnail( $designer_post->ID, "thumbnail" );
$html .= '<tr>';
$html .= '<th scope="row">View</th>';
$html .= '<td><p>'.$designer_post->post_title.'</p></td>';
$html .= '<td>';
foreach($designer_post as $test) {
$html .= 'what you want fraté';
}
$html .= '</td>';
$html .= '<td>'.$imageThumb.'</td>';
$html .= '</tr>';
}
<?php
foreach( $designer_posts as $designer_post ) {
$tags = get_the_tags($designer_post->ID);
$imageThumb = get_the_post_thumbnail($designer_post->ID,"thumbnail");
?>
<tr>
<th scope="row">View</th>
<td><p> <?php print $designer_post->post_title; ?></p></td>
<td>
<?php foreach($array_data as $data) {
print $data;
} ?>
</td>
<td> <?php print $imageThumb; ?></td>
</tr>
<?php } ?>

Select all from table and display result mysql

<?php
$query_announcement = mysql_query("SELECT * FROM announcement");
$get_announcement = mysql_fetch_assoc($query_announcement);
?>
<?php
if(mysql_num_rows($query_announcement) > 0){
$row = mysql_num_rows($query_announcement);
for($x = 1; $x<= $row; $x++){
$html = '<tr>';
$html .= '<td>'.$get_announcement['id'].'</td>';
$html .= '<td>'.$get_announcement['announce_name'].'</td>';
$html .= '<td>'.$get_announcement['announce_description'].'</td>';
$html .= '<td>'.$get_announcement['announce_location'].'</td>';
$html .= '<td>'.date('M d,Y', strtotime($get_announcement['date_start'])).'</td>';
$html .= '<td>'.date('M d,Y', strtotime($get_announcement['date_end'])).'</td>';
$html .= '<td>'.date('M d,Y', strtotime($get_announcement['date_added'])).'</td>';
$html .= '<td width="15%">
<button type="button" class="btn btn-default" title="Edit"><span class="fa fa-pencil"></span></button>
<button type="button" class="btn btn-default" title="Delete" ><span class="fa fa-trash-o"></span></button>
</td>';
$html .= '</tr>';
echo $html;
}
}
else{
echo '<tr><td colspan="8" align="center"><h3>No Announcement</h3></td></tr>';
}
?>
/this is my code but when i run it i only get the announcement with the same id, not all announcement that i created. please help thanks
You are looping the announcement but are not then pulling the next announcement.
for($x = 1; $x<= $row; $x++){
//Pull next record
$row = mysql_num_rows($query_announcement);
If you change it to the code above the for loop will start. The data will be pulled, displayed and then it will loop back around again.
your r fetching data only one time
for($x = 1; $x<= $row; $x++){
$get_announcement= mysql_fetch_assoc($query_announcement);
Try this: You need to loop through the rows to and use the each row to display them.
<?php
$query_announcement = mysql_query("SELECT * FROM announcement");
$get_announcement = mysql_fetch_assoc($query_announcement);
?>
<?php
if($get_announcement){
while($row = mysql_fetch_array($get_announcement)) {
$html = '<tr>';
$html .= '<td>'.$row['id'].'</td>';
$html .= '<td>'.$row['announce_name'].'</td>';
$html .= '<td>'.$row['announce_description'].'</td>';
$html .= '<td>'.$row['announce_location'].'</td>';
$html .= '<td>'.date('M d,Y', strtotime($row['date_start'])).'</td>';
$html .= '<td>'.date('M d,Y', strtotime($row['date_end'])).'</td>';
$html .= '<td>'.date('M d,Y', strtotime($row['date_added'])).'</td>';
$html .= '<td width="15%">
<button type="button" class="btn btn-default" title="Edit"><span class="fa fa-pencil"></span></button>
<button type="button" class="btn btn-default" title="Delete" ><span class="fa fa-trash-o"></span></button>
</td>';
$html .= '</tr>';
echo $html;
}
}
else{
echo '<tr><td colspan="8" align="center"><h3>No Announcement</h3></td></tr>';
}

Get individual form collection elements ZF2

I need to use collections to make dynamic inputs, but needed to pull out the individual elements to integrate them into a Bootstrap layout.
Unfortunately I can't find any documented way of doing this.
Could someone help-me?
Here is my form: http://pastebin.com/JGy7JEJk Fieldset used in form:
http://pastebin.com/VBbG1yyb
Form HTML: http://pastebin.com/HHaZZKsB
Form View Helper: http://pastebin.com/x7B9aPWG
You can see this in action http://protesto21.com.br/cadastro/encargo-vigencia/atualizar/2/
User admin, password p21
Thanks for all and sorry for my english
Problem solved
public function renderCollection($element)
{
$return = '';
if (count($element->getMessages()) > 0) {
$return .= '<div class="form-group has-error">';
} else {
$return .= '<div class="form-group">';
}
foreach ($element->getIterator() as $field) {
$label = ($this->isView) ? '<strong>' . $element->getLabel() . '</strong>' : $element->getLabel();
$return .= '<label class="col-md-3 control-label">' . $label . '</label>';
$return .= '<div class="col-md-8" style="margin-bottom: 20px;">';
foreach ($field->getElements() as $inputs) {
$return .= '<div style="float: left; margin-right: 20px;">';
$return .= $inputs->getLabel();
$return .= $this->renderInput($inputs);
$return .= '<div style="min-width: 265px;">';
foreach ($inputs->getMessages() as $mensagem) {
$return .= "<span class='help-block'>" . $mensagem . "</span>";
}
$return .= '</div>';
$return .= '</div>';
}
$return .= '</div>';
$element->setLabel('');
}
$return .= '</div>';
return $return;
}

PHP Change default value for Select list

I have this little issue with the price form for my Joomla backend.
The default value is always "nothing", so I just need to set "Euro" as default (see screenshot).
I'm pretty newbie with PHP, so I can't do it by myself..
Here is the part of code that realizes this form:
$htmlPrice = '<div class="jomcomdevPriceRow">';
$htmlPrice .= '<div class="control-group" style="float: left; margin-right: 10px;">';
$htmlPrice .= '<label> '.JText::_('COM_JOMCOMDEV_FIELD_PRICE_NETTO').'</label><input type="text" name="' . $this->name.'[price_netto][]" id="' . $this->id . 'ValueNetto"' . ' value="" />';
$htmlPrice .= '</div>';
$htmlPrice .= '<div class="control-group" style="float: left; margin-right: 10px;">';
$htmlPrice .= str_replace("\n", '', '<label is="jform_featured-lbl"> '.JText::_('COM_JOMCOMDEV_FIELDSET_PRICE_GROUP').'</label>'.JHtml::_('select.genericlist', JHtml::_('jdcategory.options', 'com_jomestate.price', array('onlyroot' => 0)), $this->name.'[type_id][]',"", 'value', 'text', null, true));
$htmlPrice .= '</div>';
$htmlPrice .= '</div>';
I hope I'm clear enough. Thank you in advance.
Screenshot:
http://i.stack.imgur.com/2Ix39.png
UPDATE
Hmm, I don't know really where is configured this element. Here is full code, maybe I'm looking a wrong part..
defined( '_JEXEC' ) or die( 'Restricted access' );
class JFormFieldJdPrice extends JFormField
{
/**
* #var string The form field type.
* #since 11.1
*/
public $type = 'JdAddress';
/**
* Method to get the field input markup.
*
* #return string The field input markup.
*
* #since 11.1
*/
public function getInput()
{
$id = JRequest::getInt('id');
// if(empty($id)) {
// $html = '<div style="padding: 10px; margin: 10px 0;">';
// $html .= JText::_('COM_JOMCOMDEV_PRICEADD_NOID_INFO');
// $html .= '</div>';
// return $html;
// }
$htmlPrice = '<div class="jomcomdevPriceRow">';
$htmlPrice .= '<div class="control-group" style="float: left; margin-right: 10px;">';
$htmlPrice .= '<label> '.JText::_('COM_JOMCOMDEV_FIELD_PRICE_NETTO').'</label><input type="text" name="' . $this->name.'[price_netto][]" id="' . $this->id . 'ValueNetto"' . ' value="" />';
$htmlPrice .= '</div>';
$htmlPrice .= '<div class="control-group" style="float: left; margin-right: 10px;">';
$htmlPrice .= str_replace("\n", '', '<label is="jform_featured-lbl"> '.JText::_('COM_JOMCOMDEV_FIELDSET_PRICE_GROUP').'</label>'.JHtml::_('select.genericlist', JHtml::_('jdcategory.options', 'com_jomestate.price', array('onlyroot' => 0)), $this->name.'[type_id][]',"", 'value', 'text', null, true));
$htmlPrice .= '</div>';
$htmlPrice .= '</div>';
$link = JURI::root()."index.php?option=com_jomcomdev&format=raw&task=ajax.price&name=first&id=";
$runScript = "
window.addEvent('domready', function() {
var options = {htmlPrice: '".$htmlPrice."', link: '".$link."', selector: $$('#".$this->id."')};
Comdev.price.init(options);
});
";
$document = JFactory::getDocument();
$document->addScriptDeclaration($runScript);
JText::script('COM_JOMCOMDEV_JS_BUTTON_ADD');
JText::script('COM_JOMCOMDEV_JS_BUTTON_DELETE');
JText::script('COM_JOMCOMDEV_JS_BUTTON_OPTION');
JText::script('COM_JOMCOMDEV_FIELD_PRICE_NETTO');
$html = '';
$html .= '<div id="'.$this->id.'">';
if(!empty($id)) {
$data = Main_Price::get($id, (string) $this->element['extension']);
foreach($data AS $d) {
$html .= '<div class="jomcomdevPriceRow">';
$html .= '<div class="control-group" style="float: left; margin-right: 10px;">';
$html .= '<label is="jform_featured-lbl"> '.JText::_('COM_JOMCOMDEV_FIELD_PRICE_NETTO').'</label><input type="text" name="' . $this->name.'[price_netto][]" id="' . $this->id . 'ValueNetto"' . ' value="'.$d->price_netto.'" />';
$html .= '</div>';
$html .= '<div class="control-group" style="float: left; margin-right: 10px;">';
$html .= '<label is="jform_featured-lbl"> '.JText::_('COM_JOMCOMDEV_FIELDSET_PRICE_GROUP').'</label>'.JHtml::_('select.genericlist', JHtml::_('jdcategory.options', 'com_jomestate.price', array('onlyroot' => 0)), $this->name.'[type_id][]', 'value', null, 'text', $d->type_id, true);
$html .= '</div>';
$html .= '<div class="control-group" style="padding-top: 17px;">';
$html .= '<label></label>'.JText::_('COM_JOMCOMDEV_JS_BUTTON_DELETE').'';
$html .= '</div>';
$html .= '</div>';
$html .= '<div class="clr"></div>';
}
}
$html .= '</div>';
return $html;
}
}

Text misalignment in HTML table output by PHP and displayed in Browser

function my_fav_quote_show_optin_form() {
if (!empty($_POST['my_fav_quote_email'])) {
my_fav_quote_opt_in();
}
$out2 = '';
$out = '<form action="" method="post" id="requestQuote">';
$out .= '<table style="padding="0px" width="40px">';
$out .= '<tr><td>Name:*</td><td><input type="text" name="my_fav_quote_name" id="my_fav_quote_name"/></td></tr>';
$out .= '<tr><td colspan=1></td></tr>';
$out .= '<tr><td>Email:*</td><td><input type="text" name="my_fav_quote_email" id="my_fav_quote_email"/></td></tr>';
$out .= '<tr><td colspan=2></td></tr>';
$out .= '<tr><td>Message:</td><td><textarea cols=20 rows=5 wrap="hard" name="my_fav_quote_message" id="my_fav_quote_message"></textarea></td></tr>';
$out .= '<tr><td colspan=2></td></tr>';
$out .='<tr><td colspan="2">';
if ( function_exists( 'my_fav_quote_display' ) ){
$out .= my_fav_quote_display();
}
the thing is that this is a plugin of wordpress which i am modifying for my need their is misalignment of the word "message" i.e is it is appearing too low with respect to text areabox and also cols and row syntax are not working have tried applying style top ,bottom and also tried margin but the message word is not moving from the place .please click on add to quote for seeing the widget where the problem is a link
<td style="vertical-align: top;">Message</td>

Categories