Jump to content

New to php, columns instead of raws


werushka

Recommended Posts

I am editing code to get sku numbers, for now it just prints as below:

 

SKU: 123

SKU: 322

SKU: 444

SKU: 555

SKU: 666

 

I want to put them in columns so it prints as:

 

SKU: 123      SKU: 322    SKU: 444      SKU: 555

SKU: 666      etc.              etc.

 

So it is devided into 4 columns.

 

The code that controls is:

<?php if (is_array($order->products)) {
                            $context = array(
                              'revision' => 'formatted',
                              'type' => 'order_product',
                              'subject' => array(
                                'order' => $order,
                              ),
                            );
                            foreach ($order->products as $product) {
                              $price_info = array(
                                'price' => $product->price,
                                'qty' => $product->qty,
                              );
                              $context['subject']['order_product'] = $product;
                              $context['subject']['node'] = node_load($product->nid);
                              ?>
                      <?php echo $product->model; ?><br />
                             
                          <?php }
                              }?>

 

i would appreciate some help

Link to comment
https://forums.phpfreaks.com/topic/223412-new-to-php-columns-instead-of-raws/
Share on other sites

Not tested

 

<?php
//Specify the number of columns
$max_columns = 4;

if (is_array($order->products))
{
    $context = array(
        'revision' => 'formatted',
        'type'     => 'order_product',
        'subject'  => array('order' => $order,),
    );
    
    //Start output table
    $output = "<table border=\"1\">\n";
    $recordCount = 0;
    
    foreach ($order->products as $product)
    {
        $price_info = array(
            'price' => $product->price,
            'qty' => $product->qty,
        );
        $context['subject']['order_product'] = $product;
        $context['subject']['node'] = node_load($product->nid);
    
        //generate the output
        $recordCount++;
        if($recordCount % $max_columns == 1)
        {
            //Open table row if first record of row
            $output .= "<tr>\n";
        }
        $output .= "<td>" . $product->model . "</td>\n";
        if($recordCount % $max_columns == 0)
        {
            //Close table row if last record of row
            $output .= "</tr>\n";
        }
    }
    //Close out last row if needed
    if($recordCount % $max_columns != 0)
    {
        while($recordCount % $max_columns != 0)
        {
            $output .= "<td></td>\n";
        }
        $output .= "</tr>\n";
    }
    //Close output table
    $output .= "</table>\n";
    echo $output;
}
?>

Try this:

<?php if (is_array($order->products)) {
                            $context = array(
                              'revision' => 'formatted',
                              'type' => 'order_product',
                              'subject' => array(
                                'order' => $order,
                              ),
                            );
                            foreach ($order->products as $product) {
                              $price_info = array(
                                'price' => $product->price,
                                'qty' => $product->qty,
                              );
                              $context['subject']['order_product'] = $product;
                              $context['subject']['node'] = node_load($product->nid);
                              ?>
                      <?php echo '<table border="0" cellspacing="5" cellpadding="5">';
                                  $i = 0;
                                  foreach(explode('<br />',$product->model) as $value) {
                                      echo ($i == 0) ? '<tr>' : NULL;
                                      echo '<td>' . str_replace('<br />','',$value) . '</td>';
                                      if(++$i == 4) { 
                                        echo '</tr>';
                                        $i = 0;
                                      }
                                 }
                             echo ($i != 0 && $i < 4) ? '</tr></table>' : '</table>';
                              ?><br />
                             
                          <?php }
                              }?>

 

This is assuming that $product->model produces sku's separated by line breaks.  If it produces an un-ordered list, it will have to be done differently.  Your source code will be the place to look to find that info out.

I think I come this far but still can't solve it  :-[:

<table border="0" cellspacing="5" cellpadding="5">
<?php if (is_array($order->products)) {
                            $context = array(
                              'revision' => 'formatted',
                              'type' => 'order_product',
                              'subject' => array(
                                'order' => $order,
                              ),
                            );
                            foreach ($order->products as $product) {
                              $price_info = array(
                                'price' => $product->price,
                                'qty' => $product->qty,
                              );
                              $context['subject']['order_product'] = $product;
                              $context['subject']['node'] = node_load($product->nid);
                              ?>

                      <?php;       
                                  
                                  $i = 0;
                                  echo ($i != 0 && $i < 4) ? NULL: '<tr>';
							  
                                  foreach(explode('<tr>',$product->model) as $value) {
                                      
                                      echo '<td>' . str_replace('<br />','',$value) . '</td>';
                                      if(++$i == 4) { 
                                        echo '</tr>';
                                        $i = 0;
                                      }
                                 }
                             echo ($i != 0 && $i < 4) ? '</tr>' : NULL;
                              ?>
                             
                          <?php }
                              }?></table>

The out put is

 <table border="0" cellspacing="5" cellpadding="5"> 

                      <tr><td>SKU: 123</td></tr>                             
                          							
                      <tr><td>SKU: 322</td></tr>                             
                          							
                      <tr><td>SKU: 444</td></tr>                             
                          							
                      <tr><td>SKU: 555</td></tr>                             
                          							
                      <tr><td>SKU: 666</td></tr>                             
                          </table> 

 

but it supposed to be

 

<table border="0" cellspacing="5" cellpadding="5"> 

                      <tr><td>SKU: 123</td>                            
                             <td>SKU: 322</td>                            
                             <td>SKU: 444</td>                             
                             <td>SKU: 555</td></tr>                             
                       <tr><td>SKU: 666</td></tr>                             
                          </table>  

Perhaps you can adapt this to your needs...

<?php

$my_array = array ("123","456","789","101","222","879","245","908","12345","2468","9922");

$max_columns = 4; /* set max columns for table */
$i = 0; /* initially set counter to use in starting and ending rows */
?>
<table border ="2">
<?PHP

$total_cells = count($my_array);  /* count total cells to be displayed */

$total_rows = ceil($total_cells / $max_columns);  /* calculate total rows needed */

$junk1 = $total_rows * $max_columns;  /* calculate number of empty cells in last row */

$junk2 = $junk1 - $total_cells;

if($junk2==0) { 
    $last_row = "</tr>"; 
}else{ 
    $j = 0; 
    while($j<$junk2){ 
        $last_row = $last_row . "<td></td>"; 
        $j ++; 
    } 
    $last_row = $last_row . "</tr>"; 
}
$x=0;

while($x <$total_cells) {  /* begin looping thru the results */
    if($i == 0){ 
        echo "<tr>";     
        $i ++; 
    } 
    ?>
    <td><?PHP echo $my_array[$x]; ?></td>
    <?PHP 
$x ++;
    $i ++;
    if($i > $max_columns) {   /* check if need to close row */
        echo "</tr>"; 
        $i=0; 
    }        
}
echo $last_row . "</table>"; /* clean up last row */
?> 

There is only 1 thing you need to do in order to make this work (see the first comments)....

<?php
/* THIS IS THE ONLY THING YOU NEED TO CHANGE TO MAKE IT WORK */
/* SIMPLY FILL THE VARIABLE - $my_array WITH YOUR DB CONTENT
$my_array = array ("123","456","789","101","222","879","245","908","12345","2468","9922");

/* NO NEED TO EDIT BEYOUND THIS POINT */

$max_columns = 4; /* set max columns for table */
$i = 0; /* initially set counter to use in starting and ending rows */
?>
<table border ="2">
<?PHP
$total_cells = count($my_array); /* count total cells to be displayed */
$total_rows = ceil($total_cells / $max_columns); /* calculate total rows needed */
$junk1 = $total_rows * $max_columns; /* calculate number of empty cells in last row */
$junk2 = $junk1 - $total_cells;
if($junk2==0) { 
$last_row = "</tr>"; 
}else{ 
$j = 0; 
while($j<$junk2){ 
	$last_row = $last_row . "<td></td>"; 
	$j ++; 
} 
$last_row = $last_row . "</tr>"; 
}
$x=0;
while($x <$total_cells) { /* begin looping thru the results */
if($i == 0){ 
	echo "<tr>"; 
	$i ++; 
} 
?>
<td>SKU: <?PHP echo $my_array[$x]; ?></td>
<?PHP 
$x ++;
$i ++;
if($i > $max_columns) { /* check if need to close row */
	echo "</tr>"; 
	$i=0; 
} 
}
echo $last_row . "</table>"; /* clean up last row */
?> 

I have simplified my code

<?php { foreach ($order->products as $product) { echo $product->model;  }}?>

this code alone gives me all the products in the DB. in a single raw. But I need to implement this to only this code

$my_array = array ("123","456","789","101","222","879","245","908","12345","2468","9922");

but I don't know how to make my line of code implemented to my_array

 

 

just a test, replace this

<?php { foreach ($order->products as $product) { echo $product->model;  }}?>

with this...

<?php
foreach ($order->products as $product) { 
$my_array[] = $product->model;  
}
echo "<PRE>";
print_r($my_array);
echo "</pre>";
exit();

?>

thank you very much litebearer

the following code works perfectly

<?php
foreach ($order->products as $product) { 
$my_array[] = $product->model;  
}
$max_columns = 4; /* set max columns for table */
$i = 0; /* initially set counter to use in starting and ending rows */
?>
<table border ="2">
<?php
$total_cells = count($my_array); /* count total cells to be displayed */
$total_rows = ceil($total_cells / $max_columns); /* calculate total rows needed */
$junk1 = $total_rows * $max_columns; /* calculate number of empty cells in last row */
$junk2 = $junk1 - $total_cells;
if($junk2==0) { 
$last_row = "</tr>"; 
}else{ 
$j = 0; 
while($j<$junk2){ 
	$last_row = $last_row . "<td></td>"; 
	$j ++; 
} 
$last_row = $last_row . "</tr>"; 
}
$x=0;
while($x <$total_cells) { /* begin looping thru the results */
if($i == 0){ 
	echo "<tr>"; 
	$i ++; 
} 
?>
<td><?php echo $my_array[$x]; ?></td>
<?php 
$x ++;
$i ++;
if($i > $max_columns) { /* check if need to close row */
	echo "</tr>"; 
	$i=0; 
} 
}
echo $last_row . "</table>"; /* clean up last row */
?> 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.