Easiest way to Format MySQL Data to Next Column in PHP - php

I have my data being output to a span currently... this is how it looks:
Now, when i remove the span and place a div there i am given this output:
This is desired, but I want to set a height to my page and have the data show up in as little as 3 columns. How would I do this? I have searched everywhere online but can't seem to find anything that shows a solution.
I did read that some use javascript for the format but i am still clueless on even this option.
My desired output would look like this:

If you know how many items you want in a column then you can seperate them out into individual divs and then float those divs to the left to get them to be next to each other.
<div style='float:left'>
//Items go here
</div>
<div style='float:left'>
//Items go here
</div>
etc.
If you figure out how many items your query returned, say using mysql_num_rows() and divide by 3 you can tell how many to put in each column.
Also be sure to clear the floats afterwards, so like this:
<div style="clear:both"></div>
Sometimes this is necessary as there will be random issues if this is not put there.

What you are describing can be solved with styling only. You have several divs that must be displayed in columns. The easiest way is floating them to the left, and setting the width for 1/3 of the parent. If you want 4 columns, set the with to 1/4 of the parent, and so on.
<div class='sqlResult' style="float:left;width:33%;">
<a href='#'>$key</a>
</div>
Also as other answers mentioned, don't use duplicated ids. Always use classes. If you need to target each div individually, give it a unique id, such as "category_1", "category_2", and so on.

This should work
<table><tr>
<?php $count=0; $total=mysqli_stmt_num_rows($sql)-1; $idxcount=0; $limit=10; while($row = mysqli_fetch_array($sql)): $key = $row['Keyword_Name']; ?>
<?php if($count == 0){ echo '<td>';} ?>
<span>
<?php echo $key; ?>
</span>
<?php if($total == $idxcount): ?>
</td>
<?php elseif($count == $limit): ?>
</td>
<?php $count=0; else: $count++; ?>
<?php endif; $idxcount++; ?>
<?php endwhile; ?>
</tr></table>

Related

Automatic generation of <div> tag into Html webpage

i have these lines of code:
<div class="signals">
<ul>
<li>First Signal</li>
<li>Second Signal</li>
<li>Third Signal</li>
</ul>
</div>
<div id="signal1" style="display:none;">
<p style="color:#fff">First comment for #signal1 id - it will open in a fancybox -.</p>
<div id="signal2" style="display:none;">
<p style="color:#fff">Second comment for #signal2 id - it will open in a fancybox -.</p>
</div>
<div id="signal3" style="display:none;">
<p style="color:#fff">Third comment for #signal3 id - it will open in a fancybox -.</p>
</div>
Here it is the jsfiddle code: JsFiddle
Right now when i want to show different comments, i open my html file and edit the "id #signal , adding more id (or deleting them) when i need it.
The problems is: the signals can be more than the three that are showed up, or even less.
So my question is:
There is a way to generate automatically the divs that i need in a second sheet, where i will insert the comment and all the id's? (a sort of backend)
For example: if one day i need just 2 signals, i will create the #signal1 and #signal2 div, i'll insert the comments and save the secondary sheet.
When i do that, the primary sheet with the html stuff, will show 2 "li" lines:
First Signal
Second Signal
and when i click on of them, the fancybox will open and show the comment, just as the code into the jsfiddle.
I don't know much of php, but there is a way i can do that using it? Or there is a better way?
Hope i can learn from your help.
It seems that you need PHP foreach loop.
You can write the comments in a PHP array, and read them using PHP foreach.
Here is the sample code:
<?php
$commentsLabels = array('First Signal','Second Signal');
$commentsTexts = array('First comment for #signal1 id - it will open in a fancybox.','Second comment for #signal2 id - it will open in a fancybox.');
//You could use a 2D array to store both comments Labels and comments Texts in one array, but since you are new to PHP, I put them in 2 different arrays to make it easy.
//You can add more comments texts/labels to the arrays in double/single quotes separated with ','
//For now, the arrays are filled manually. in future, you can add a DB to store comments and fetch the values from there.
$counter = 0;
//we need a counter to make the comment ids dynamic
?>
<!--GENERATING LABELS-->
<div class="signals">
<ul>
<?php
foreach ($commentsLabels as $commentLabel) { //loop starts
$counter++; //increasing $counter by 1, for each iteration
//We have to add HTML tags outside the php code block.
?>
<li><?php echo $commentLabel; ?></li>
<?php
} //loop ends
?>
</ul>
</div>
<!--GENERATING POPUPS-->
<?php
$counter = 0; //reset counter
foreach ($commentsTexts as $commentText) { //loop starts
$counter++;
?>
<div id="signal<?php echo $counter; ?>" style="display:none;"><p style="color:#fff"><?php echo $commentText; ?></p></div>
<?php
} //loop ends
?>
I hope this helps you start. :-)

ForEach multiply issue

This is probably a stupid gotcha that I'm overlooking but I'm hoping one of you can help me!
I've got a loop to list a grid of Products in my DB.
So far so good, everything is displaying roughly OK except this one little issue.
Within a list I'm doing the following:
<ul>
<?php $i=0; foreach ($products as $product) : $i++; ?>
<li <?php
if(($i%4) ==0){
echo 'class="last"';
} elseif($i%2==0){
echo 'class="second"';
}
?>>
// Then I've got the image thumbnail etc coming in....
All looks good except for the LAST row...
So for instance if I have 8 products... the first 7 will display on the page correctly, but then there is a gap at the end where the 8th product moves onto the next page.
At first I thought it was CSS widths or something but it's not. Even if I have 20 products...always the last row only shows 3 across and puts the last product on the next page.
Any ideas anyone?
Cheers M
<ul>
<?php $i=count($products); ?>
<li
<?php
if(($i%4) ==0){
echo 'class="last"';
} elseif($i%2==0){
echo 'class="second"';
}
?>>
Let's try it..

Add a unique ID to Drupal Views list groupings

I'm using Views 3 in Drupal 7 to output a list of fields and using a grouping field to generate separate lists. I need each of the groupings to have a unique ID attribute applied to the < ul > but they don't by default.
As far as I'm aware I would need to edit the views-view-list.tpl.php template but I don't know how to achieve a unique ID per iteration.
Can anyone help please?
easiest way I can think of off the top of my head...
<?php print $wrapper_prefix; ?>
<?php if (!empty($title)) : ?>
<h3><?php print $title; ?></h3>
<?php endif; ?>
<ul id="<?php echo uniqid(); ?>">
<?php foreach ($rows as $id => $row): ?>
<li class="<?php print $classes_array[$id]; ?>"><?php print $row; ?></li>
<?php endforeach; ?>
</ul>
<?php print $wrapper_suffix; ?>
that would go in your views-view-list.tpl.php file.
For future reference:
Put a div around everyting in view-views-list.tpl.php. You can (ab-)use the $title to generate unique (but consistent) id's.
Do it like this:
<?php $id = str_replace('FOR ALL UNWANTED CHARS','',$title); ?>
<div id="<?php print strtolower($id); ?>">
You can use the $view->dom_id variable. It is a unique id for that views instance.
In your .tpl.php file:
<?php print $view->dom_id; ?>
From comments in modules\views\theme\theme.inc:
<?php
// It is true that the DIV wrapper has classes denoting the name of the view
// and its display ID, but this is not enough to unequivocally match a view
// with its HTML, because one view may appear several times on the page. So
// we set up a hash with the current time, $dom_id, to issue a "unique" identifier for
// each view. This identifier is written to both Drupal.settings and the DIV
// wrapper.
?>

How to use PHP variables in my ExpressionEngine template for counting?

Hokay, so. Here's my embedded template, chilling:
<? $i=0; ?>
{exp:channel:entries
channel="products"
dynamic="no"
entry_id="{embed:ids}"
}
<? $i++; ?>
{exp:playa:parents
field_id="25"
limit="1"
}
<!-- product -->
{if no_parents}
<? $i--; ?>
{/if}
{/exp:playa:parents}
{if no_results}
No results!
{/if}
{/exp:channel:entries}
<? if ($i === 0 ) { echo 'No products found!'; } ?>
The logic I had for this $i variable was to get an accurate reading of whether any results have been output. "Result" in this sense refers to what gets output by Playa.
The exp_channel_entries's no_results test only gets triggered if {embed:ids} is empty or the embedded ids don't correspond to entries in the channel. If the entries method returns entries but none of the results have a parent entry, nothing gets output -- and I needed a way to determine this, and I thought "Hmm, PHP should be able to do this, right?"
The desired outcome is that 'No products found!' gets output when $i = 0 but for some reason, $i is always 0 regardless of what entries get spit out.
Oh, and before you ask: YES, PHP is indeed enabled. Example: Below, {embed:ids} = 41|78|79|80|81|87|106. When set to OUTPUT, the PHP tags just get printed in the source:
<? $i=0; ?>
<? $i++; ?>
<!-- product -->
<? $i++; ?>
<!-- product -->
<? $i++; ?>
<!-- product -->
<? $i++; ?>
<!-- product -->
<? $i++; ?>
<? $i--; ?>
<? $i++; ?>
<!-- product -->
<? if ($i === 0 ) { echo 'No products found!'; } ?>
If I switch PHP parsing to INPUT the tags get processed, but $i = 0 every time.
I added an echo $i; after $i=0, $i++, and $i--. With PHP set to OUTPUT, as before, the statements just get output in the page source. With PHP set to INPUT, I get this string of values: 0 1 1 1 1 1 01
So my questions to you, StackOverflow community, is:
1) Why does PHP in OUTPUT mode just output the PHP tags without processing them?
2) How can I keep count of the number of product parents being output?
I couldn't tell you why your PHP isn't being parsed when switched to output (never seen that before), but I do think there's a simpler way to do this:
{exp:query sql="SELECT child_entry_id FROM exp_playa_relationships WHERE parent_field_id = 25 AND child_entry_id IN({embed:ids})"}
{exp:playa:parents field_id="25" entry_id="{child_entry_id}" limit="1"}
<!-- product -->
{/exp:playa:parents}
{if no_results}<p>No products found!</p>{/if}
{/exp:query}
This query will only return IDs of entries which do indeed have parents. The only thing you'll have to do is change your passed ids embed from using pipes to commas.
You could also try this:
{exp:channel:entries channel="products" entry_id="0{exp:query sql="SELECT parent_entry_id FROM exp_playa_relationships WHERE parent_field_id = 25 AND child_entry_id IN({embed:ids})"}|{parent_entry_id}{/exp:query}" dynamic="no"}
<!-- product -->
{if no_results}<p>No products found!</p>{/if}
{/exp:channel:entries}
I realize that this thread is old but I'm posting just in case someone else has similar problems and can't follow Derek's awesome solution due to architectural issues.
My issues were solved with -
Adding full php start tags (with php)
Remove all braces and going with the alternative syntax
foreach(items as item):
...
endforeach;
Changing permissions on the file to 755.
Changing the group owner of the file to the default web user
Deleting all template manager entries (if that doesn't work, just edit the file on template manager itself)
Hope this helps.

PHP/MySQL 2 'while' statements from 1 mysql query?

I was wondering if it's possible to have 2 'while' statements using the same 'mysql_query'?
I'm using a jquery thumb gallery with the results pulled in from a database. The gallery requires that the images are grouped together in an unordered list, with the text/overlayed content grouped together in hidden divs which need to be separate from the unordered list. The reason for doing this is because there are many potential results that could go in here and bringing them in randomly seems to make sense.
Here's the code I'm currently using: Any help greatly appreciated, S.
<div id="banner-wrap">
<div id="banner" class="gallery">
<ul class="galleryBar">
<?php
$homeB=mysql_query("select * from istable where fpGallery = '1' ORDER BY RAND() LIMIT 0, 5");
while($homeG=mysql_fetch_array($homeB)) {
$linkcode = $homeG['title'];
$linkcode = str_replace(" ","",$linkcode);
echo '
<li>
<a href="'.$wwwUrl.'images/'.$homeG['image'].'" rel="'.$linkcode.'">
<img src="'.$wwwUrl.'images/tn/'.$homeG['image'].'" width="75" height="55" alt="'.$homeG['title'].'" />
</a>
</li>
';
}
echo '</ul>';
echo '</div>';
while($homeGal=mysql_fetch_array($homeB)) {
echo '
<div id="'.$linkcode.'" class="overlay">
<h3>'.$homeGal['title'].'</h3>
<h4>'.$homeGal['location'].'</h4>
</div>
';
}
?>
</div>
Try to reset the iterator of the query result by calling mysql_data_seek($homeB, 0) between the two while loops.
WHERE `column1` = 'value1' AND `column2` = 'value2'
Your unindented code is hard to follow but it appears that you're asking whether mysql_fetch_array() starts again when it reaches the last row. The answer to that is no.
Can you rewind the result set? In theory, you can (find mysql_data_seek() in the manual). But there's no need to make it so complicate: just read data once, store it into an array and loop the array as many times as you need.

Categories