I am having the worst time trying to create this, it should be simple. I have an $info array that consists of $person=>$personInfoArray and I am trying to say, for the first six, do one column, for the next x amount, do another column.
What I have is:
$infoCounter = 0;
foreach($info as $person => $information){
$infoCounter++;
echo '<tr>';
if($infoCounter <= 5){
echo '<td>'.$person.'</td>';
echo '<td>'.$information['Extension'].'</td>';
}elseif($infoCounter > 5){
echo '<td>'.$person.'</td>';
echo '<td>'.$information['Extension'].'</td>';
}
echo '</tr>';
}
I am essentially trying to create a table that looks like:
Name Extension Name Extension
--------------------------------------------------------------------
Some one 54545 Name 9785
Some One else 54212 Something else 44121
But I am getting:
Name Extension Name Extension
--------------------------------------------------------------------
Some one 54545
Name 9785
Some One else 54212
Something else 44121
Thoughts? This should be ridiculously easy.
See that you are creating a <tr> for each person, try doing something like this:
$infoCounter = 0;
foreach($info as $person => $information){
if ($infoCounter % 2 == 0) {
echo '<tr>';
}
echo '<td>'.$person.'</td>';
echo '<td>'.$information['Extension'].'</td>';
if ($infoCounter % 2 == 0) {
echo '</tr>';
}
$infoCounter++;
}
Check that % its the modulus
You will always have "2" columns with this code. I think what you are after is this.
$counter = 0;
foreach($info as $person => $information){
if($counter == 0) echo "<tr>";
echo "<td>$person</td>";
echo "<td>$information</td>";
$counter += 2;
if($counter == 4) {
$counter = 0;
echo "</tr>";
}
}
Keep in mind this is crude and off the top of my head. Not sure why you want 4 columns for 2 column information, other than formatting.
Related
I am running into a problem rendering a star rating and was hoping I can get some extra eyeballs on my problem. I have the normal rating working just fine with whole numbers but I am struggling to display the half star. For example I created a service that provides me with a rating 0-5 so I get a value like 2.5, 3 or 5 etc...
Before I go and create a switch case and create an svg for each variation I was hoping to get a little a pointer. Below is what I have currently, any tips would be greatly appreciated.
<?php
for ($i = 1; $i <= $totalRating; $i++) {
if($starRating < $i ) {
echo "<img src=\"/icons/star-empty.svg\">";
}
else {
echo "<img src=\"/icons/star.svg\">";
}
}
?>
Ideally I would like to add a condition at the end of the loop and check for the half and echo "";
There is probably an easier way to do it but this works, checks if $starRating is a float and then rounds it up and checks against $i to place the half star in the correct position.
<?php
$totalRating = 5;
$starRating = 2.5;
for ($i = 1; $i <= $totalRating; $i++) {
if($starRating < $i ) {
if(is_float($starRating) && (round($starRating) == $i)){
echo "<img src=\"/icons/star-half.svg\">";
}else{
echo "<img src=\"/icons/star-empty.svg\">";
}
}else {
echo "<img src=\"/icons/star.svg\">";
}
}
?>
You can verify if the value of $starRating is an integer, doing something like this (considering only half values):
<?php
for ($i = 1; $i <= $totalRating; $i++) {
if ($starRating < $i ) {
echo "<img src=\"/icons/star-empty.svg\">";
} elseif(is_int($starRating) === false) {
echo "<img src=\"/icons/star_half.svg\">";
} else {
echo "<img src=\"/icons/star.svg\">";
}
}
?>
If you want to show stars with a more precise value you can create images with the float values in the name, like "star-3.svg" (representing a .3 float value, and do something like this:
<?php
for ($i = 1; $i <= $totalRating; $i++) {
if ($starRating < $i ) {
echo "<img src=\"/icons/star-empty.svg\">";
} elseif(is_int($starRating) === false) {
echo "<img src=\"/icons/star-" . floatval($starRating)) . ".svg\">";
} else {
echo "<img src=\"/icons/star.svg\">";
}
}
?>
But in this case you need to take care to only receive float values with one number (2.2, 3.4, etc.).
I hope it helps...
I'd like to fill a table vertically. For example one name on one row, the age on the second row alternately, and limit the table to 4 columns:
Is this possible with this kind of code?
Here is my Code:
<?php
$age = array("26","16","17","19","24","30");
$name = array("adam","andrew","tim","mike","don","eddy");
echo "<table border=1>";
for($i=0;$i<count($age);$i++)
{
if ($i > 0 && $i % 4 == 0)
{
echo "</tr><tr>";
}
echo "<td>";
echo $name[$i];
echo "</td>";
echo "<td>";
echo $age[$i];
echo "</td>";
}
echo "</tr>";
?>
Please try this. It is not the most elegant way but will give you an idea and maybe improve your answer. I will suggest you maybe use multi-dimensional array to store age and name together.
$age = array("26","16","17","19","24","30");
$name = array("adam","andrew","tim","mike","don","eddy");
$ageChunks = array_chunk($age, 4); //divide age array into chunks of 4
$nameChunks = array_chunk($name, 4); //divide name array into chunks of 4
//print table data
function printData($td)
{
echo "<td> ".$td." <td>";
}
//print table row. Assumption here is $nameChunks and $ageChunks are same length.
function printRow($nameChunks, $ageChunks)
{
foreach ($nameChunks as $key=>$val) {
echo "<tr>";
array_map("printData", $nameChunks[$key]);
echo "</tr><tr>";
array_map("printData", $ageChunks[$key]);
echo "</tr>";
}
}
//print table
function printTable($nameChunks, $ageChunks)
{
echo "<table>";
printRow($nameChunks, $ageChunks);
echo "</table>";
}
printTable($nameChunks, $ageChunks);
you need to change condition
if ($i > 0 && $i % 4 == 0) to if ($i > 0 && $i % 2 == 0) because you print 2 columns per iteration
I am trying to limit the printing of table cells to 3 per row. This worked in one example, but clearly not working when I tried to use the same code somewhere else in the site. This is the code:
$n=3;
echo "<table cellpadding='10' cellspacing='10' style='margin-right:-70px;'><tr>";
$users_count = count($users);
for($i=0; $i<$users_count;$i++)
{
$temp = array();
$temp = $users[$i];
echo "<td>";
echo "<div id='kitchen_box'>";
echo "<div id='kitchen_box_details'>";
echo "<h4>".$temp->fullname . "</h4><br>";
if(strcmp($temp->address, '') == 0)
echo $temp->city;
else
echo $temp->address.", ".$temp->city;
echo "</div>";
echo "<div id='kitchen_box_pic'><img id='kitchen_image' src='".$temp->profilepic."' /></div>";
echo "</div>";
echo "</td>";
if($i != 0){
if($i % $n == 0 && $i != $users_count-1){
echo "</tr><tr>";
}
else{
echo ""; //if it is the last in the loop - do not echo
}
}
}
echo "</table>";
I can't see why this wouldn't work! I would really appreciate support on the matter :)
There are several issues with your code. But your main issue is that you're using a zero-based increment, but doing a 1-based check. So, a table of your the results of an $i!=0 && $i%$n==0 goes like this:
$i $result
0 false
1 false
2 false
3 true
So, you see, the result closes the row after the fourth, not the third cell. To fix this, change the line to:
if($i % $n == $n-1 && $i != $users_count-1){
You should also include a closing </tr> tag with your closing </table> tag.
Incidentally, you shouldn't give the same ID to multiple elements on a page. Each of your kitchen_box and kitchen_box_div DIV tags will have the same ID. If you want this for CSS, use classes. Otherwise, you might try adding the value of $i to each ID.
Nitpicking on request:
The line $temp = array(); seems a little pointless, especially since you don't want $temp to be an array, but an object.
The else{ echo ""; } lines are also redundant.
You don't need the if($i != 0) check now because that case will not pass the next test anymore.
Otherwise the code seems fine to me.
I think the problem is that your $i starts at zero.
Let's look at your conditions before creating a new row :
if($i != 0){
if($i % $n == 0 && $i != $users_count-1)
If $i = 0, the first condition isn't matched. Then the second isn't for $i = 1 and $i = 2. And then your script creates another ... in your first row before matching your if conditions for the first time.
I guess you could move this part of the code right after the beginning of the for instructions :
for($i=0; $i<$users_count;$i++)
{
if($i != 0)
{
if($i % $n == 0 && $i != $users_count-1){
echo "</tr><tr>";
}
else{
echo ""; //if it is the last in the loop - do not echo
}
}
// Echo your <td> ... </td>
}
echo "</tr></table>";
So basically I have a script that is suppose to display my code into rows of 3, and create a new row. However, it is all displayed in one column. Here's the code.
while($mysql2 = mysql_fetch_array($sql2)) {
$prodlogo = $mysql2['logo'];
$prodid = $mysql2['id'];
$prodname = $mysql2['name'];
$x = 1;
if($x==1) {
echo "<tr>";
}
echo "<td><img src=images/productpics/$prodlogo></br><a href=viewproduct.php?pid=$prodid>$prodname</a></td>";
$x++;
if($x==3) {
echo "</tr>";
$x = 1;
}
}
move the $x out of the loop,otherwise you are reset it to one in every loop
each <td> will be a separate column. I would suggest you format it thus.
while($mysql2 = mysql_fetch_assoc($sql2)) {
foreach($mysql2 as $row) {
echo "<tr>";
echo "<td>".$row['logo']."</td><td>$".row['id']."</td><td>".$row['name']."</td>";
echo "</tr>";
}
}
also you need to use mysql_fetch_assoc() to get an associative array you can access like $row['key']
You may need to instantiate $x = 1 outside of your while loop - you're resetting it to 1 each time you're running it.
Using modulo might be good too:
if (($x % 3) == 0) {
echo "<tr>"
}
echo "<td><img src=images/productpics/$prodlogo></br><a href=viewproduct.php?pid=$prodid>$prodname</a></td>";
if (($x % 3) == 0) {
echo "</tr>"
}
You wouldn't need to worry about resetting $x to 1...
I am tweaking a facebook album class that loops and echos data. The problem is that when it echos it loops into one column and I want to separate it into 2 columns. This is what it looks like now:
foreach($json->data as $v)
{
echo "<a class='ImageLink' rel='lightbox[photos]' href = '".$v->source."'><img style='margin:10px;' width='110px' src='".$v->picture."' /></a>";
}
I am trying to do something like so:
$count = count($json->data);
$half_count = $count/2;
echo "<ul class='float:left;'>";
$counter = 0;
foreach($json->data as $v)
{
if ($counter == $half_count +1){echo "<ul class='float:left;'>";}
echo "<li>". $v->picture ."</li>";
if ($counter == $half_count){ echo "</ul>";}
$counter++;
}
echo "</ul>";
But when I use the count function on $json->data and echo that it gives me an array. Please help;
"But when I use the count function on $json->data and echo that it gives me an array." <- Count will always return an int.
Try the following correction to your code:
$count = count($json->data);
$half_count = ceil($count / 2); // make sure to ceil to an int. This will have your first column 1 larger than the second column when the count is odd
echo '<ul style="float:left">';
$counter = 0;
foreach($json->data as $v) {
echo '<li>' , $v->picture , '</li>';
$counter += 1;
if ($counter == $half_count && $count != 1) {
echo '</ul><ul style="float:right">';
}
}
echo "</ul>";