Bootstrap grid and layout output - html

Suppose that I have a block of HTML:
<figure>
<img/>
</figure>
and I need to wrap them in Twitter Bootstrap 3 columns, like <div class="col-md-3">{content}</div>. I need to make rows every 4 times in the loop (to sum the 12 columns fitting the row) to make the grid.
How is the best way to achieve this?
Does Twig offer that solution easily? I would like to just pass what the each column classes I need and the "function" work out to split the columns automagically.
I'm using https://stackoverflow.com/a/16428403/1110456 as a solution, but seems that has a better way to do that. I currently use CakePHP and a framework and PHP language.

You need to try something like this. It will:
Get a count of how many objects you have in your view
Set the object counter and column counter to zero
If this is your first column, start a <div class="row">
Display the content
Increment the column counter by one
If this is column 4, or if this is your final item, close the row div
and reset the column counter.
$count = count($models);
$i = 0;
$column = 0;
foreach ($models as $model) {
if ($column == 0) {
echo "<div class='row'>";
}
echo "<div class='col-md-3'>" . "Content" . "</div>";
$column = $column + 1;
if((++$i === $count && $column !== 4) || ($column == 4)) {
echo "</div>";
$column = 0;
}
}

Related

MYSQL How can I make dropdown list?

I created a new table in workbench where I want to put a season of a soccer championship.
I don't want to write down every time the team's names, can you help me pls, how can I make like a dropdown list (or set of values, what are the only options for that column), when I begin to type "Bay..." its automatically offer "Bayern München" as the only option.
I don't know how much will this help, but I have made a dropdown list for something else and I hope my way might give you an idea:
foreach ($dbo->query($sql) as $row) //Array or records stored in $row
{
if($row[id]==10)
{
echo "<option value=$row[id] selected>$row[name]</option>";
}
else
{
echo "<option value=$row[id]>$row[name]</option>";
}
}
echo "</select>"; // Closing of list box
As the same answer above, but using short-hand ternary operator instead of if-else statement, and don't forget to use ' or " when reference to object string index.
foreach ($dbo->query($sql) as $row) //Array or records stored in $row
{
printf("<option value='%s' %s>%s</option>", $row['id'], $row['id'] == 10 ? 'selected' : '', $row['name']);
}
}
echo "</select>"; // Closing of list box

Way to make row count be 0 if no date match and second,third,etc while loop row counts

<?php
if($result = $db->query("SELECT * FROM table")) {
if($count = $result->num_rows) {
echo '<p class="lead">Number of callbacks for today is: ', $count, '</p>';
while($row = $result->fetch_assoc()) {
if ( $row['field'] == date("Y-m-d") ) {
echo $row['otherfield'], '<br>';
}else{
echo "Nothing", '' , '<br>';
}
}
}
}
?>
I have a simple field date verses server date test used in conjunction with a while loop to bring up any callbacks each day if they are due. My issue is that the row will show a nothing for every row so I'll have twenty(example) rows of the text 'nothing' display. I know I could simply place everything in a function and then use the if/else statement outside of the function to display callbacks or not display callbacks....but is there a way to reset the loop counter to zero if the first if/else test fails? I tried placing the $count = 0; in the second else clause but this did not work.

How can I choose only 2 columns without changing the SELECT query?

I am generating a table with this code:
if ($arch = $pdo->prepare("SELECT one, two, three, four FROM numbers)) {
$arch ->execute();
$data = $arch->fetchAll();
echo '<table>';
foreach ($data as $row){
echo '<tr>';
foreach ($row as $col){
$col=nl2br($col);
echo '<td>'.$col.'</td>';
}
echo '</tr>';
}
echo '</table>';
}
}
This populates a table with 4 columns.
How can I choose only 2 columns without changing the SELECT query?
In other words, how can I choose the columns I want to populate the table? for example I would like to use only "three" and "four"...
I read about fetchAll(PDO::FETCH_COLUMN, 0) but this seems to pick only one column. The same goes for fetchColumn(). I want to pick more than one so I can populate my table with only the columns I need.
Just add a selector on the second foreach, to filter the columns you want:
....
foreach($row AS $col => $value){
if($col == 'your_column_name'){
echo '<td>'.nl2br($value).'</td>';
}
}
....
Make sure you have your 'fetchmode' on associative, which makes sure the pdo-result object contains a associative array with key-indexes.
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
Alternatively, you can specify the columns you want as an array beforehand, and iterate over that array rather than the columns in the row.
$columns = array('I_like_this_column', 'this_one_too');
foreach ($columns as $colName)
echo '<td>' . nl2br($row[$colName]) . '</td>';
This has the added advantage of letting you specify the order the columns will appear in your output without changing your query.
And, as with #stUrb's answer, you need to turn on PDO::FETCH_ASSOC for this to work.

PHP/ARRAY/HTML TABLE -- How come this repeats the same thing across the whole <td>?

Im trying to turn an array of friends names: $friends['name'] from the Facebook Graph API into a table with 4 columns across with one persons name in each box.
The problem:
I have it making a table and putting peoples names in the cells of the table, however, it repeats each persons name across all 4 in a row instead of putting a unique name in each box. However the next row starts with a new name.
I would greatly appreaciate anybody who can help point out where Ive gone wrong and help me correct it:
<?
$friends_url = "https://graph.facebook.com/".$user_id."/friends?access_token=".$access_token;
$friends_json = file_get_contents($friends_url);
$friends_data = json_decode($friends_json, true);
$friends_total = count($friends_data['data']);
$cols = 4;
for ($i = 0; $i < $friends_total; $i++)
{
$friends = $friends_data['data'][$i];
echo "<tr>";
for ($c=0; $c<$cols; $c++)
{
$n = $i+$c;
if ( $n < $friends_total)
{
echo "<td>".$friends['name']."</td>";
}
else
{
echo "<td></td>";
}
}
echo "</tr>";
$i += ($c-1);
}
?>
Because you're looping 4 times for each friend and your loop counter for 'columns' is inside that loop, try something more like this:
$cols = 4;
$current_col = 0;
echo "<tr>";
foreach ($friends_data['data'] as $friend)
{
echo "<td>".$friend['name']."</td>";
if (($current_col++)%$cols == 0){
echo "</tr><tr>";
}
}
while (($current_col++)%$col !=0){
echo "<td> </td>";
}
echo "</tr>";
This loops through all the friends, keeping a counter of how many have been printed. After 4 (/or whatever $cols is set to) it spits out a new row
When the friends are all printed out, it starts a new loop to fill in the end of the last row with empty cells, then ends the row

How can I display images similar to Tumblr's photosets?

I'm converting my wife's blog over to Wordpress and I want to display image galleries similar to how they're displayed on Tumblr. Here's an example of the layout:
http://bobbyandmaura.com/post/8195960363/photoset_iframe/bobbyandmaura/tumblr_lp2nebJFEW1qhd8ae/500
I can handle the markup and CSS for displaying the images. What I need help with is understanding how I can create this dynamically. Tumblr is smart enough to dynamically display different quantities of images while still always filling all of the space. Here's another example with fewer image:
http://bobbyandmaura.com/post/6700400507/photoset_iframe/bobbyandmaura/tumblr_ln23gi8EqU1qhd8ae/500
Hopefully I can use math to create a dynamic solution so I don't have to manually create a bunch of different possibilities.
OK. Solved. I got a hack to show WP gallery as Tumblr photoset. It is not as customizable as Tumblr, this will only show the first image as the cover image(larger) and rest images in small grid like set.
You need to edit wp-includes/media.php file. If your theme as any other gallery file, you have to edit that. Edit at your own risk as this is core file of WP. If you update your WP in future, you have to do this again. Sorry, I have no time to write a plugin.
Search for "foreach ( $attachments as $id => $attachment )" in wp-includes/media.php and change the following (replace the foreach loop)
$ccg = 1;
foreach ( $attachments as $id => $attachment ) {
if($ccg == 1)
{
$link = isset($attr['link']) && 'file' == $attr['link'] ? wp_get_attachment_link($id, large, false, false) : wp_get_attachment_link($id, $size, true, false);
}
else
{
$link = isset($attr['link']) && 'file' == $attr['link'] ? wp_get_attachment_link($id, $size, false, false) : wp_get_attachment_link($id, $size, true, false);
}
$output .= "<{$itemtag} class='gallery-item'>";
$output .= "
<{$icontag} class='gallery-icon'>
$link
</{$icontag}>";
if ( $captiontag && trim($attachment->post_excerpt) ) {
$output .= "
<{$captiontag} class='wp-caption-text gallery-caption'>
" . wptexturize($attachment->post_excerpt) . "
</{$captiontag}>";
}
$output .= "</{$itemtag}>";
if ( ( $columns > 1 && ++$i % $columns == 1) || $ccg == 1 )
$output .= '<br style="clear: both" />';
$ccg++;
}
Demo gallery can be found at http://ontrip.in/the-ca-cur-badi-forest-resort-gorumara/
You may need to modify your Gallery columns number. You may select the cover image by alternating the image order in Gallery insert box.
My friend Victor Gonzáles developed Aurum to manage proportions and have that nice effect. You can get it here: https://github.com/aficiomaquinas/Aurum-CSS
You then additionally group the images by similar proportions(w/h) with a tolerance of 0.2. So that <0.2 is one group < 0.4 another, < 0.6, etc.
Then have a random chance of spawning them in 1,2,3 columns / row.