Pulling content from MySql Database with PHP and display in 3 columns - php

I'm pulling content from MySQL database and I would like to display such content in 3 columns. I have managed to achieve it, but not all the content gets pulled. Even when I change it to 4 columns, it displays more content but still not all of them.
There must be a way to check how much content it is and display it correctly. Any help would be much appreciated.
This is what I have so far:
(I'm supposed to display 52 items from database, but only 38 or so get displayed)
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<div id="wrapper">
<div id="page">
<div id="content">
<div id="report_content">
<div id="top_bar">
<h1 class="report_title"></h1>
<h2 class="report_subtitle"></h2>
<div id="criteria"></div>
</div>
<div>
<div>
<div id="t1" class="results resultList">
<?php if ($row_detail_vendors) { $temp_name='' ; ?>
<table>
<?php $cols=4 ; do { ?>
<tr>
<?php for($i=1;$i<=$cols;$i++){ // All the rows will have $cols columns even if // the records are less than $cols $row_detail_vendors=m ysql_fetch_assoc($detail_vendors) ?>
<td> <a href="partner_info.php?idu=<?php echo $row_detail_vendors['shortcut']; ?>">
<?php echo ucfirst(strtoupper($row_detail_vendors['hotel_name'])); ?></a>
<br> <span><?php echo $row_detail_vendors['city']; ?>, <?php echo ucfirst($row_detail_vendors['country']); ?></span>
<br/>
<br/>
</a>
</td>
<?php } ?>
</tr>
</div>
<!--marketing-->
<?php } while ($row_detail_vendors=m ysql_fetch_assoc($detail_vendors)); ?>
<?php }?>
</div>
</div>
</div>
<!--marketing partners-->
</div>
<!--content-->
</div>
<!--page-->
</body>

Would advise:
<div id="t1" class="results resultList">
<table>
<?php
$cols=4;
$i = 1;
while($row_detail_vendors=mysql_fetch_assoc($detail_vendors)) {
if($i % $cols == 1){
echo "<tr>"; // start column wrapper
} ?>
<td><?php echo strtoupper($row_detail_vendors['hotel_name'])); ?>
<br /><span><?php echo $row_detail_vendors['city']; ?>, <?php echo ucfirst($row_detail_vendors['country']); ?></span>
<br/>
<br/>
</td>
<?php
if( $i % $cols == 0){
echo "</tr>"; // End column wrapper
}
$i++;
} ?>
</table>
</div>
<!--marketing-->

Related

PHP Bootstrap row loop every 3 posts

Trying to display the custom posts on my archive page within a bootstrap row containing 3 columns then starting a new row, got the code but new to PHP and dont know where to put the content.
<?php
//Columns must be a factor of 12 (1,2,3,4,6,12)
$numOfCols = 3;
$rowCount = 0;
$bootstrapColWidth = 12 / $numOfCols;
?>
<div class="row">
<?php
foreach ($rows as $row){
?>
<div class="col-md-4"<?php echo $bootstrapColWidth; ?>">
<div class="thumbnail">
<img src="user_file/<?php echo $row->foto; ?>">
</div>
</div>
<?php
$rowCount++;
if($rowCount % $numOfCols == 0) echo '</div><div class="row">';
}
?>
</div>
<div class="embed-container">
<?php the_field('podcast_embed_link'); ?>
</div>
<h3><?php the_title(); ?></h3>
<p><b><?php echo $date->format( $format_out );?></b></p>
<p><?php the_field('description'); ?></p>
<?php if( get_field('thumbnail') ): ?>
<img src="<?php the_field('thumbnail'); ?>" />
<?php endif; ?>
<?php endwhile; // end of the loop. ?>
</div>
</div>
</div><!-- #content -->
Here is the code for the page archive.podcasts.php, where would i add the custom fields within the row loop?
First of all, you don't need to close and open row tag each 3 items. If you leave the code like this:
<div class="row">
<?php
foreach ($rows as $row){
?>
<div class="col-md-<?php echo $bootstrapColWidth; ?>">
<div class="thumbnail">
<img src="user_file/<?php echo $row->foto; ?>">
</div>
</div>
<?php
}
?>
</div>
you will get the same effect, but without the separation that a row tag involves. Notice that the line involving "col-md-4" has already changes in order to not create wrong col size usage.
In this part of code:
<div class="col-md-4"<?php echo $bootstrapColWidth; ?>">
You must get wrong bootstrap class like col-md-41, col-md-412.
Correct you code by this way:
<div class="col-md-<?php echo $bootstrapColWidth; ?>">

php code to display rows in two different display

I create two design to display news .
first DIV Display BIGGER images and Separate css class and display only two rows.
Second DIV Display Smaller images and Separate css class and display five rows..
I select rows in asc order from query.
my query is = select * from news order by priority asc.
My Only Need is TO Display two rows in First div and then display another rows in Second DIV.
IS This is possible using if else or any other condition...how to do this.........
but keep all div tag as it is bcoz of page design purpose......
<!-- FIRST DIV -->
<div class="post">
<div class="buffer">
<?php foreach($top2 as $top){ ?>
<div class="content"> <img src="bhaskarcms/uploads/<?php echo $top['photo']; ?>" style="width:285px; height:100px;" />
<h2><?php echo htmlspecialchars_decode($top['headline']); ?></h2>
</div>
<?php } ?>
<!--<p class="details2">8 Comments / Read More</p>-->
</div>
</div>
<!-- end post -->
<!-- SECOND DIV -->
<!-- begin post -->
<div class="post">
<div class="buffer">
<?php foreach($top2 as $top){ ?>
<div class="content1"><img src="bhaskarcms/uploads/<?php echo $top['photo']; ?>" />
<h2><?php echo htmlspecialchars_decode($top['headline']); ?></h2>
</div>
<?php } ?>
</div>
</div>
<!-- end post -->
Yes, it's possible. Assuming your $top2 array has standard numerical/sequential keys, then:
$split = 2;
for($i = 0; $i < $split; $i++) {
... print $top2[0] and $top2[1]
}
for ($i = $split; $i < count($top2); $i++) {
... print the rest of the rows
}
Can use array_slice()
<!-- FIRST DIV -->
<div class="post">
<div class="buffer">
<?php $arr = array_slice($top2, 0, 2) ?>
<?php foreach($arr as $top){ ?>
<div class="content"> <img src="bhaskarcms/uploads/<?php echo $top['photo']; ?>" style="width:285px; height:100px;" />
<h2><?php echo htmlspecialchars_decode($top['headline']); ?></h2>
</div>
<?php } ?>
<!--<p class="details2">8 Comments / Read More </p>-->
</div>
</div>
<!-- end post -->
<!-- SECOND DIV -->
<!-- begin post -->
<div class="post">
<div class="buffer">
<?php $arr = array_slice($top2, 2) ?>
<?php foreach($arr as $top){ ?>
<div class="content1"><img src="bhaskarcms/uploads/<?php echo $top['photo']; ?>" />
<h2><?php echo htmlspecialchars_decode($top['headline']); ?></h2>
</div>
<?php } ?>
</div>
Add a count that you increment in the foreach loop, then break when its over two:
<!-- FIRST DIV -->
<div class="post">
<div class="buffer">
<?php
$rowCount = 0;
foreach($top2 as $top){ ?>
<div class="content"> <img src="bhaskarcms/uploads/<?php echo $top['photo']; ?>" style="width:285px; height:100px;" />
<h2><?php echo htmlspecialchars_decode($top['headline']); ?></h2>
</div>
<?php
$rowCount++;
if($rowCount > 2){
break;
}
} ?>
<!--<p class="details2">8 Comments / Read More</p>-->
</div>
</div>
<!-- end post -->

Editing a php scripèt divs to output a table

Hey guys ok so i'm wokring on a clients Weekly Newsletter i'm working with Joomla 2.5.19 and using the enterprise version of acymailling to send it out. I'm kicking my heading in at the moment because of outlook, i'm using a module from Jreviews that publishes the latest reviews submitted to the site in the newsletter, it all works fine except in outlook.
the out put of the script is meant to be a 2x2table with the 4 latest reviews in it. the only prob is outlook seems to hates me using Div for a table and stacks the 2x2 table into a verticle kaotic mess.
the code i'm trying to edit is:
'>
<?php /* root element for the items */ ?>
<div class="jrModuleItems <?php echo $orientation . ' jrThumbnail'.ucfirst($tn_position); ?>">
<?php /* new page starts here */
$pages = array_chunk($reviews,$limit);
$j=0;
foreach($pages AS $page):
?>
<div class="jr-results jrResults jrModuleContainer jrReviewsModule">
<?php $i=0;
while(!empty($page)):
$i++; $j++; $review = array_shift($page); ?>
<?php
// Process link title
$listing_title = ($listing_title_chars && mb_strlen($review['Listing']['title'])>$listing_title_chars) ? $Text->truncate($review['Listing']['title'],$listing_title_chars) : $review['Listing']['title'];
$review_title = ($review_title_chars && mb_strlen($review['Review']['title'])>$review_title_chars) ? $Text->truncate($review['Review']['title'],$review_title_chars) : $review['Review']['title'];
$link_title = str_replace('{listing_title}',$listing_title,$link_title_format);
$link_title = str_replace('{review_title}',$review_title,$link_title);
// Create the thumbnail
$tn_show and $mainMediaThumb = $Media->thumb(Sanitize::getVar($review,'MainMedia'),array('listing'=>$review,'size'=>$tn_size,'mode'=>$tn_mode,'css_size'=>true));
?>
<?php $lastItem = ($i == $columns) ? ' jrLastItem' : ''; ?>
<div class="jrModuleItem<?php echo $lastItem; ?>" style="width: <?php echo $item_width; ?>%; padding-right: <?php echo $item_padding; ?>%;">
<?php if($show_numbers):?><div class="jrModuleItemNumber"><?php echo $j;?>.</div><?php endif;?>
<?php if($tn_show && $mainMediaThumb && $tn_position != 'bottom'):?>
<!-- Listing Thumbnail -->
<div class="jrModuleItemThumbnail">
<?php echo $Html->sefLink($mainMediaThumb,$review['Listing']['url']);?>
<?php // Uncomment line below to show reviewer avatar. You can comment or remove the thumbnail code above
// echo $Community->avatar($review);
?>
</div>
<?php endif;?>
<div class="jrModuleItemContent">
<!-- Listing Title -->
<div class="jrModuleItemTitle">
<?php echo $Html->sefLink($link_title,$review['Listing']['url']);?>
<?php if(Sanitize::getString($review['Listing'],'tag')):?>
<span class="jrComponentLabel jrStatusLabel jrBlue">
<?php echo Sanitize::getString($review['Listing'],'tag');?>
</span>
<?php endif;?>
</div>
<!-- Rating -->
<?php if ( $review['Criteria']['state'] == 1 ):?>
<div class="jrOverallRatings">
<?php if($review['Review']['editor'] == 1):?>
<?php
$rating_stars = $Rating->drawStars($review['Rating']['average_rating'], $this->Config->rating_scale, 'editor');
$rating_value = $Rating->round($review['Rating']['average_rating'],$this->Config->rating_scale);
?>
<div class="jrOverallEditor" title="<?php __t("Editor rating"); ?>">
<div class="jrRatingStars"><?php echo $rating_stars ?></div>
<span class="jrRatingValue"><?php echo $rating_value?></span>
</div>
<?php else:?>
<?php
$rating_stars = $Rating->drawStars($review['Rating']['average_rating'], $this->Config->rating_scale, 'user');
$rating_value = $Rating->round($review['Rating']['average_rating'],$this->Config->rating_scale);
?>
<div class="jrOverallUser" title="<?php __t("User rating"); ?>">
<div class="jrRatingStars"><?php echo $rating_stars ?></div>
<span class="jrRatingValue"><?php echo $rating_value?></span>
</div>
<?php endif;?>
</div>
<?php endif;?>
<!-- Reviewer name -->
<div class="jrModuleItemReviewer">
<span class="reviewer"><?php __t("Reviewed by");?> <?php echo $Community->screenName($review);?></span>
</div>
<?php if($fields): ?>
<!-- Custom Fields -->
<div class="jrModuleFields">
<?php
foreach ($fields as $field):
$field = trim($field);
$field_value = $CustomFields->field($field,$review);
?>
<?php if($field_value != ''):?>
<div class="jrModuleFieldDiv <?php echo lcfirst(Inflector::camelize($field)); ?>">
<span class="jrModuleFieldTitle"><?php echo $CustomFields->label($field, $review); ?>: </span>
<span class="jrModuleFieldValue"><?php echo $field_value; ?></span>
</div>
<?php endif;?>
<?php endforeach; ?>
</div>
<?php endif;?>
<?php if($show_comments && trim($review['Review']['comments'])!=''):?>
<!-- Review Comments -->
<div class="jrModuleItemInfo">
<?php
// Uncomment line below to show review title
// echo '<strong>' . $review['Review']['title'] . '</strong><br />';
?>
<span class="comments">"<?php echo $Text->truncateWords($review['Review']['comments'],$comments_words,'...');?>"</span>
</div>
<?php endif;?>
</div>
<?php if($tn_show && $mainMediaThumb && $tn_position == 'bottom'):?>
<!-- Listing Thumbnail -->
<div class="jrModuleItemThumbnail">
<?php echo $Html->sefLink($mainMediaThumb,$review['Listing']['url']);?>
<?php // Uncomment line below to show reviewer avatar. You can comment or remove the thumbnail code above
// echo $Community->avatar($review);
?>
</div>
<?php endif;?>
</div>
<?php /*end of row , start new row*/
if(!empty($page) && ($i == $columns || $total == $j)):?>
<div class="jrDivider"></div>
<?php $i=0; endif;?>
<?php endwhile;?>
</div>
<?php endforeach; /* new page ends here */?>
</div><?php /* end items root element */?>
Does any one have the slightest idea how i could turn this into a for loop that outputs a table?
The quickest path from A to B is to edit the attached code to render a table versus stacked divs.
* EDIT *
The answer to your comment isn't so simple as replace all of 'A' with 'B.' A div is a "self-contained" HTML element while a table is a grouping with syntax rules.
An HTML table is constructed like so:
<table>
<thead>
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
</thead>
<tbody>
<tr>
<td>Row 1 - Column 1</td>
<td>Row 1 - Column 2</td>
</tr>
<tr>
<td>Row 2 - Column 1</td>
<td>Row 2 - Column 2</td>
</tr>
</tbody>
</table>
The foreach loop in your code should create a row through each iteration. Which means you need to render your table, thead and tbody tags outside of this looping code. Inside the loop, you render a new row each iteration, which requires an opening/closing tag for the row and an opening/closing for each column.
Hope this helps.

How to use printout extension in my view page to print view page?

I have created a yii based project. I have a view where we show data from a database. I want to print that page. I searched and found the printout extension for printing and I copied the 'print' directory to /protected/extensions, but we can not understand how we could use it on my view page to print it.
My view page:
<div class="row">
<div class="row-column1">
<?php echo CHtml::label('Reg. Number. :','student_enroll_no'); ?>
<?php echo $info->student_enroll_no;?>
</div>
<div class="row-column2">
<?php echo CHtml::label('Adm. Date :','student_adm_date'); ?>
<?php if($info->student_adm_date != NULL)
echo date('d-m-Y',strtotime($info->student_adm_date));?>
</div>
</div>
<div class="row">
<div class="row-column1">
<?php echo CHtml::label('Student Name :','student_first_name'); ?>
<?php echo $info->student_first_name;?>
</div>
<div class="row-column2">
<?php echo CHtml::label('Enroll No. :','student_mobile_no'); ?>
<?php echo $info->student_mobile_no;?>
</div>
</div>
<div class="row">
<div class="row-column1">
<?php echo CHtml::label('Fathers Name :','student_last_name'); ?>
<?php echo $info->student_last_name;?>
<!--<?php echo CHtml::label('Gender :','student_gender'); ?>
<?php echo $info->student_gender;?>-->
</div>
<div class="row-column3">
<?php echo CHtml::label('Mothers Name :','student_mother_name'); ?>
<?php echo $info->student_mother_name;?>
<!-- <?php echo CHtml::label('Date of Birth :','student_dob'); ?>
<?php if($info->student_dob != NULL)
echo date('d-m-Y',strtotime($info->student_dob));?>-->
</div>
</div>
<div class="row">
<div class="row-column1">
<?php echo CHtml::label('Gender :','student_gender'); ?>
<?php echo $info->student_gender;?>
<!--<?php echo CHtml::label('Course :','student_transaction_course_id'); ?>
<?php
echo !empty($model->student_transaction_course_id) ? $model->relCourse->course_name : 'N/A';
?>-->
</div>
<div class="row-column2">
<?php echo CHtml::label('Date of Birth :','student_dob'); ?>
<?php if($info->student_dob != NULL)
echo date('d-m-Y',strtotime($info->student_dob));?>
<!--<?php echo CHtml::label('Year :','student_academic_term_period_tran_id'); ?>
<?php echo $model->Rel_student_academic_terms_period_name->academic_term_period;?>-->
</div>
</div>
<div class="row">
<div class="row-column1">
<?php echo CHtml::label('Category :','title'); ?>
<?php echo $info->title;?>
</div>
<div class="row-column2">
<?php echo CHtml::label('Email ID :','student_email_id_1'); ?> <?php echo $info->student_email_id_1; ?>
</div>
</div>
<div class="row">
<div class="row-left">
<?php echo CHtml::label('Course :','student_transaction_course_id'); ?>
<?php
echo !empty($model->student_transaction_course_id) ? $model->relCourse->course_name : 'N/A';
?>
<!-- <?php echo CHtml::label('Blood Group :','student_living_status'); ?>
<?php echo $info->student_living_status; ?>-->
</div>
<div class="row-column3">
<!-- <?php echo CHtml::label('Nationality :','student_transaction_nationality_id'); ?>
<?php if($model->student_transaction_nationality_id!=null)
echo $model->Rel_Nationality->nationality_name;
?>-->
<?php echo CHtml::label('Year :','student_academic_term_period_tran_id'); ?>
<?php echo $model->Rel_student_academic_terms_period_name->academic_term_period;?>
</div>
</div>
<div class="row">
<div class="row-left">
<?php echo CHtml::label('Subjects :','languages_known1'); ?>
<?php
$knwLang = "";
if($lang->languages_known1)
$knwLang = $lang->Rel_Langs1->languages_name;
if($lang->languages_known2)
$knwLang .= ", ".$lang->Rel_Langs2->languages_name;
if($lang->languages_known3)
$knwLang .= ", ".$lang->Rel_Langs3->languages_name;
echo $knwLang;
?>
</div>
</div>
<div class="row last">
<?php echo ('_____________________Note -Click on edit and update DoB , Subjects and upload photo ,Enroll no. .'); ?>
</div>
The documentation for this extension says it is tuned for the dataGrid widget. However it seems you are not using the dataGrid widget so what you can do is override the default printedElement property.
For example:
Wrap all the elements you wish to print in a div and give it an id of #printme and add the following to your view.
<?php
$this->widget('application.extensions.print.printWidget', array(
'cssFile' => 'print.css',
'printedElement'=>'#printme',
)
)
);
?>
NOTE: Don't forget to add/edit the print.css file.
Read the documentation for more options:
http://www.yiiframework.com/extension/printout

Setting an interval on do / while from 5 to 5

I've made a repeat region with dreamweaver and the result is this:
<div>
<?php do { ?>
<div class="slide">
<img src="<?php echo $row_Recordset1['imagemUrl']; ?>" />
<h2><?php echo $row_Recordset1['titleProj']; ?></h2>
</div>
<?php while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
</div>
No problem here, it works fine.
The thing is, I'm trying to use only 5 slides for each page, something like this:
<div>
<div class="page">
<?php do { ?>
<div class="slide">
<img src="<?php echo $row_Recordset1['imagemUrl']; ?>" />
<h2><?php echo $row_Recordset1['titleProj']; ?></h2>
</div>
<?php
$i = 0;
$r = $i % 5;
$i++;
if ( $r == 0 ) {
?>
</div> <!-- closes div.page -->
<div class="page"> <!-- adds a new div.page -->
<? } ?>
<?php while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
</div>
It's obviously not working, but hopefully you'll see what I'm trying to reach.
Thanks in advance!
UPDATE:
The result i get is:
<div class="page">
<div class="slide">...</div>
<div class="slide">...</div>
<div class="slide">...</div>
<div class="slide">...</div>
<div class="slide">...</div>
<div class="slide">...</div>
<div class="slide">...</div>
<div class="slide">...</div>
</div>
I would like it to be:
<div class="page">
<div class="slide">...</div>
<div class="slide">...</div>
<div class="slide">...</div>
<div class="slide">...</div>
<div class="slide">...</div>
</div>
<div class="page">
<div class="slide">...</div>
<div class="slide">...</div>
<div class="slide">...</div>
</div>
You're setting $i to 0 on every iteration. Place the $i = 0 before the do-while loop.
You should probably also place $i++ before $r = $i % 5; to get the result you require.
EDIT: You're also missing a closing brace before your while statement (typo?)
Hence, the complete code:
<div>
<div class="page">
<?php
$i = 0;
do { ?>
<div class="slide">
<img src="<?php echo $row_Recordset1['imagemUrl']; ?>" />
<h2><?php echo $row_Recordset1['titleProj']; ?></h2>
</div>
<?php
$i++;
$r = $i % 5;
if ( $r == 0 ) {
?>
</div> <!-- closes div.page -->
<div class="page"> <!-- adds a new div.page -->
<?php
}
} while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
</div>
the piece you gave is not a valid php code.
the line here:
<h2><?php echo $row_Recordset1['titleProj']; ?></h2>
opens a php tag without closing it before the next one you open.. so a first step would be to change that line to
<h2><?php echo $row_Recordset1['titleProj']; ?></h2>
also you missed closing a block, here:
<? } ?>
you should close both the if and the do/while block (so another })
another problem is the fact that on every step you change $i to 0, so every time $r will be 0 also
so in conclusion: take your problems step by step as you have more than one
<div>
<div class="page">
<?php for ($i = 0; $row_Recordset1 = mysql_fetch_assoc($Recordset1); $i++) { ?>
<div class="slide">
<img src="<?php echo $row_Recordset1['imagemUrl']; ?>" />
<h2><?php echo $row_Recordset1['titleProj']; ?></h2>
</div>
<?php if ( $i % 5 == 0 && $i ) { ?>
</div> <!-- closes div.page -->
<div class="page"> <!-- adds a new div.page -->
<?php } ?>
<?php } ?>
</div>
</div>
Something like this would loop through 5 times adding a new <div>content</div> to the variable $html. You can then echo the variable in your html body.
$html = "";
for ($i=0;$i<5;$i++) {
$html .= "<div class=\"page\">". $content[$i] . "</div>\n";
}
Additions:
<?PHP
while ($row_Recordset = mysql_fetch_assoc($Recordset1)) {
$html .= "<div class='page'>\n";
for ($i=0;$i<5;$i++) {
$html .= "<div class='slide'>". $row_Recordset[$i]['column'] . "</div>\n";
}
$html .= "</div>\n";
}
?>
This code will loop through the recordset populating $html on each iteration. Try echo $html and you will have an output structured as you want.

Categories