I am trying to use TCPDF library to have a PDF file that supports Arabic letters. I have created a table and get the data from MYSQL database.
<?php
// Include the main TCPDF library (search for installation path).
require_once('TCPDF/config/tcpdf_config.php');
require_once('TCPDF/tcpdf.php');
class PDF extends TCPDF{
var $tablewidths;
var $headerset;
var $footerset;
function morepagestable($lineheight=8) {
// some things to set and 'remember'
$l = $this->lMargin*2;
$startheight = $h = $this->GetY();
$startpage = $currpage = $this->page;
// calculate the whole width
foreach($this->tablewidths as $width) {
$fullwidth += $width;
}
// Now let's start to write the table
$row = 0;
while($data=mysql_fetch_row($this->results)) {
$this->page = $currpage;
// write the horizontal borders
$this->Line($l,$h,$fullwidth+$l,$h);
// write the content and remember the height of the highest col
foreach($data as $col => $txt) {
$this->page = $currpage;
$this->SetXY($l,$h);
$this->MultiCell($this->tablewidths[$col],$lineheight,$txt,0,$this->colAlign[$col]);
$l += $this->tablewidths[$col];
if($tmpheight[$row.'-'.$this->page] < $this->GetY()) {
$tmpheight[$row.'-'.$this->page] = $this->GetY();
}
if($this->page > $maxpage)
$maxpage = $this->page;
unset($data[$col]);
}
// get the height we were in the last used page
$h = $tmpheight[$row.'-'.$maxpage];
// set the "pointer" to the left margin
$l = $this->lMargin*2;
// set the $currpage to the last page
$currpage = $maxpage;
unset($datas[$row]);
$row++ ;
}
// draw the borders
// we start adding a horizontal line on the last page
$this->page = $maxpage;
$this->Line($l,$h,$fullwidth+$l,$h);
// now we start at the top of the document and walk down
for($i = $startpage; $i <= $maxpage; $i++) {
$this->page = $i;
$l = $this->lMargin*2;
$t = ($i == $startpage) ? $startheight : $this->tMargin;
$lh = ($i == $maxpage) ? $h : $this->h-$this->bMargin;
$this->Line($l,$t,$l,$lh);
foreach($this->tablewidths as $width) {
$l += $width;
$this->Line($l,$t,$l,$lh);
}
}
// set it to the last page, if not it'll cause some problems
$this->page = $maxpage;
}
connect($host='xxxx',$username='xxxx',$passwd='xxxx',$db='xxxxx')
{
$this->conn = mysql_connect($host,$username,$passwd) or die( mysql_error() );
mysql_select_db($db,$this->conn) or die( mysql_error() );
return true;
}
function query($query){
$this->results = mysql_query($query,$this->conn);
$this->numFields = mysql_num_fields($this->results);
}
function mysql_report($query,$dump=false,$attr=array()){
foreach($attr as $key=>$val){
$this->$key = $val ;
}
$this->query($query);
// if column widths not set
if(!isset($this->tablewidths)){
// starting col width
$this->sColWidth = (($this->w-$this->lMargin*2-$this->rMargin))/$this->numFields;
// loop through results header and set initial col widths/ titles/ alignment
// if a col title is less than the starting col width / reduce that column size
for($i=0;$i<$this->numFields;$i++){
$stringWidth = $this->getstringwidth(mysql_field_name($this->results,$i)) + 8 ;
if( ($stringWidth) < $this->sColWidth){
$colFits[$i] = $stringWidth ;
// set any column titles less than the start width to the column title width
}
$this->colTitles[$i] = mysql_field_name($this->results,$i) ;
switch (mysql_field_type($this->results,$i)){
case 'int':
$this->colAlign[$i] = 'L';
break;
default:
$this->colAlign[$i] = 'L';
}
}
// loop through the data, any column whose contents is bigger that the col size is
// resized
while($row=mysql_fetch_row($this->results)){
foreach($colFits as $key=>$val){
$stringWidth = $this->getstringwidth($row[$key]) + 6 ;
if( ($stringWidth) > $this->sColWidth ){
// any col where row is bigger than the start width is now discarded
unset($colFits[$key]);
}else{
// if text is not bigger than the current column width setting enlarge the column
if( ($stringWidth) > $val ){
$colFits[$key] = ($stringWidth) ;
}
}
}
}
foreach($colFits as $key=>$val){
// set fitted columns to smallest size
$this->tablewidths[$key] = $val;
// to work out how much (if any) space has been freed up
$totAlreadyFitted += $val;
}
$surplus = (sizeof($colFits)*$this->sColWidth) - ($totAlreadyFitted);
for($i=0;$i<$this->numFields;$i++){
if(!in_array($i,array_keys($colFits))){
$this->tablewidths[$i] = $this->sColWidth + ($surplus/(($this->numFields)-sizeof($colFits)));
}
}
ksort($this->tablewidths);
if($dump){
Header('Content-type: text/plain');
for($i=0;$i<$this->numFields;$i++){
if(strlen(mysql_field_name($this->results,$i))>$flength){
$flength = strlen(mysql_field_name($this->results,$i));
}
}
switch($this->k){
case 72/25.4:
$unit = 'millimeters';
break;
case 72/2.54:
$unit = 'centimeters';
break;
case 72:
$unit = 'inches';
break;
default:
$unit = 'points';
}
print "All measurements in $unit\n\n";
for($i=0;$i<$this->numFields;$i++){
printf("%-{$flength}s : %-10s : %10f\n",
mysql_field_name($this->results,$i),
mysql_field_type($this->results,$i),
$this->tablewidths[$i] );
}
print "\n\n";
print "\$pdf->tablewidths=\n\tarray(\n\t\t";
for($i=0;$i<$this->numFields;$i++){
($i<($this->numFields-1)) ?
print $this->tablewidths[$i].", /* ".mysql_field_name($this->results,$i)." */ \n\t\t":
print $this->tablewidths[$i]." /* ".mysql_field_name($this->results,$i)." */\n\t\t";
}
print "\n\t);\n";
exit;
}
} else { // end of if tablewidths not defined
for($i=0;$i<$this->numFields;$i++){
$this->colTitles[$i] = mysql_field_name($this->results,$i) ;
switch (mysql_field_type($this->results,$i)){
case 'int':
$this->colAlign[$i] = 'R';
break;
default:
$this->colAlign[$i] = 'L';
}
}
}
mysql_data_seek($this->results,0);
$this->Open();
$this->setY($this->tMargin);
$this->AddPage();
$this->morepagestable($this->FontSizePt);
$this->Output();
}
}
$pdf = new PDF(PDF_PAGE_ORIENTATION,PDF_UNIT,PDF_PAGE_FORMAT,true, 'UTF- 8', false);
$pdf->SetFont('aealarabiya', '', 14, '', false);
$pdf->connect('xxxxxxxxxx','xxxxxxxxxxxxxx','xxxxxxxxxxxxx','xxxxxxxxxxxxx');
$attr=array('titleFontSize'=>24,'titleText'=>'THIS IS MY PDF FILE');
$pdf->mysql_report("SELECT * FROM Student_Table",false,$attr);
?>
I am OK with the table that is created with my code. The Arabic data retrieved is as the following?how can I solve it ?
Your problem is very simple bro, you need to choose the correct database encoding and do the query with it , but before going for that , I think this is my duty to tell you YOU DON'T HAVE TO USE "mysql" YOU HAVE TO USE "mysqli" ,now let us solve the problem :
set your database encoding to (utf8_general_ci). take a backup don't forget all the old stuff will be destroyed if you change the charset of the database do this if it's new or empty
after mysqli connect do this $db->set_charset("utf8"); or if you still
want to work with mysql use this mysql_set_charset("utf8");
Here is the edited code:
connect($host='xxxx',$username='xxxx',$passwd='xxxx',$db='xxxxx')
{
$this->conn = mysql_connect($host,$username,$passwd) or die( mysql_error() );
mysql_select_db($db,$this->conn) or die( mysql_error() );
mysql_set_charset("utf8");
return true;
}
But, like what i told you , you did it in wrong way , use mysqli and do it with full OOP not some time and in other Not !
Related
I've got an array with specifications. I want each specification to become a column. I am having trouble with working this out though.
$specifications = new Specifications();
$columnCounter = 1;
foreach ($specifications as $specificationId => $specification) {
$column = PHPExcel_Cell::getColumnByNumber($columnCounter);
$objPHPExcel
->getActiveSheet()
->getColumnDimension($column)
->setAutoSize(true)
;
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue($column.'1', $specification['value'])
;
$columnCounter++;
}
The PHPExcel::getColumnByNumber() is of course an imaginary function. Though I am wondering how others do this and how best to address this.
$book = new PHPExcel();
$book->setActiveSheetIndex(0);
$sheet = $book->getActiveSheet();
$sheet->setTitle('Sets');
$xls_row = 5;
$xls_col = 3;
foreach($specifications as $specificationId => &$specification)
{
$adr = coord($xls_row, $xls_col);
$sheet->setCellValueExplicit($adr, $specification->title, PHPExcel_Cell_DataType::TYPE_STRING);
$sheet->getColumnDimension(coord_x($xls_col))->setAutoSize(true);
$xls_col++;
}
// convert a 0-based coordinate value into EXCEL B1-format
function coord_x($x)
{
if($x<26) $x = chr(ord('A')+$x);
else
{
$x -= 26;
$c1 = $x % 26;
$c2 = intval(($x - $c1)/26);
$x = chr(ord('A')+$c2).chr(ord('A')+$c1);
}
return $x;
}
// convert X,Y 0-based cell address into EXCEL B1-format pair
function coord($y,$x)
{
return coord_x($x).($y+1);
}
I'm programming a script that inserts a text on an image, it works but not entirely, ie if a user adds a line break in the textarea is fine, but if all the text is on one line looks bad . This is my code
$str="this is a string inserted by an user";
$img_width= 500;
$img_height= 500;
$font_size = 1;
$txt_max_width = intval(0.6 * $img_width);
do {
$font_size++;
$p = imagettfbbox($font_size,0,$font,$str);
$txt_width=$p[2]-$p[0];
} while ($txt_width <= $txt_max_width);
$y = $img_height * 0.4;
$x = ($img_width - $txt_width) / 2;
$white = imagecolorallocate($img, 255, 255, 255);
imagettftext($img, $font_size, 0, $x, $y, $white, $font, $str);
imagepng($img);
imagedestroy($img);
Look at this for understand http://app.xskarx.com
PS: sorry for my bad english
What you want to do, is to split the string into smaller block or chunks, for that, you have chunk_split in PHP.
You will use it this way:
// initialize variables
$final_string = false;
$size_of_the_chunk = 20;
$str = "_this_is_a_very_long_string__this_is_a_very_long_string__this_is_a_very_long_string__this_is_a_very_long_string__this_is_a_very_long_string__this_is_a_very_long_string_";
$length_of_string = strlen( $str );
// work only if necessary
if ( $length_of_string > $size_of_the_chunk ) {
$final_string = chunk_split( $str, $size_of_the_chunk );
} else {
$final_string = $str;
}
The main problem you may have, is that the string will get split on random places, at least random from the perspective of the user, and so, the fragments may not make much sense. Try to improve the user interface by informing that they should use line breaks.
function ApplyLineBreaks(strTextAreaId) {
var oTextarea = document.getElementById(strTextAreaId);
if (oTextarea.wrap) {
oTextarea.setAttribute("wrap", "off");
}
else {
oTextarea.setAttribute("wrap", "off");
var newArea = oTextarea.cloneNode(true);
newArea.value = oTextarea.value;
oTextarea.parentNode.replaceChild(newArea, oTextarea);
oTextarea = newArea;
}
var strRawValue = oTextarea.value;
oTextarea.value = "";
var nEmptyWidth = oTextarea.scrollWidth;
function testBreak(strTest) {
oTextarea.value = strTest;
return oTextarea.scrollWidth > nEmptyWidth;
}
function findNextBreakLength(strSource, nLeft, nRight) {
var nCurrent;
if(typeof(nLeft) == 'undefined') {
nLeft = 0;
nRight = -1;
nCurrent = 64;
}
else {
if (nRight == -1)
nCurrent = nLeft * 2;
else if (nRight - nLeft <= 1)
return Math.max(2, nRight);
else
nCurrent = nLeft + (nRight - nLeft) / 2;
}
var strTest = strSource.substr(0, nCurrent);
var bLonger = testBreak(strTest);
if(bLonger)
nRight = nCurrent;
else
{
if(nCurrent >= strSource.length)
return null;
nLeft = nCurrent;
}
return findNextBreakLength(strSource, nLeft, nRight);
}
var i = 0, j;
var strNewValue = "";
while (i < strRawValue.length) {
var breakOffset = findNextBreakLength(strRawValue.substr(i));
if (breakOffset === null) {
strNewValue += strRawValue.substr(i);
break;
}
var nLineLength = breakOffset - 1;
for (j = nLineLength - 1; j >= 0; j--) {
var curChar = strRawValue.charAt(i + j);
if (curChar == ' ' || curChar == '-' || curChar == '+') {
nLineLength = j + 1;
break;
}
}
strNewValue += strRawValue.substr(i, nLineLength) + "\n";
i += nLineLength;
}
oTextarea.value = strNewValue;
oTextarea.setAttribute("wrap", "");
}
This function accepts the ID of the textarea as its parameter and whenever there is word wrap, it pushes a new line break into the textarea. Run the function in the form submit and you will get the text with proper line breaks in the server side code.
This way the text will appear exactly as the user sees it before the image is made. There is no need for the user to guess how the lines will break.
This function was found within another answer: finding "line-breaks" in textarea that is word-wrapping ARABIC text
$lines = file("res_example.txt");
$resArr = array();
foreach ($lines as $line_num => $line) {
$columns = explode("\t", $line);
$raws = $columns['1'];
$hits = $columns['2'];
$names = $columns['0'];
$models = $columns['3'];
$colors = $columns['4'];
$allModels[$models] = 1;
$resArr[] = array(
name => $names,
score => $raws,
hit => $hits,
model => $models,
color => $colors
);
}
$seqArr = array('A', 'T', 'C', 'G');
$randseq = array();
for ($i = 0; $i < 1000; $i++) {
$randseq[] = $seqArr[array_rand($seqArr)];
}
$res = "";
echo "<div id=\"coltext\" style=\"font-family:monospace;\">";
foreach ($allModels as $modName => $value) {
echo "<input ModelName=$modName type=\"checkbox\"
checked==\"TRUE\" onclick=\"toggle.apply(this)\" />$modName";
}
echo "<hr />";
$score = rawtransform($raw);
foreach ($randseq as $index => $nuc) {
$index = $index + 1;
foreach ($resArr as $hitArr) {
$hit = $hitArr['hit'];
$raw = $hitArr['score'];
$model = $hitArr['model'];
$colval = $hitArr['color'];
$score = rawtransform($raw);
$color = getcolor($score, $colval);
if (($hit+3) == $index) {
echo "</span>";
}
if ($hit == $index) {
echo "<span class=$model Title=\"position:$index,score:$raw\"
style=\"background:$color;\" color=\"$color\">";
//problem when theres overlap !?
}
}
echo $nuc;
if (($index%50)==0){
echo"<br />";
}
}
echo "</div>";
function rawtransform($raw) {
return (int)($raw/50)-9;
}
function getcolor($score,$ArrayModelval)
{
switch ($score){
// working. test each color.
case 1: /*500-550(Raw Score)*/
$col=$ArrayModelval;
return"hsl( $col,100%,90%)";
break;
case 2: //550-600
$col=$ArrayModelval;
return "hsl( $col,100%,85%)";
break;
case 3: //600-650
$col=$ArrayModelval;
return "hsl( $col,100%,85%)";
break;
case 4: //650-700
$col=$ArrayModelval;
return"hsl( $col,100%,80%)";
break;
case 5: //700-750
$col=$ArrayModelval;
return"hsl( $col,100%,70%)";
break;
case 6: //750-800
$col=$ArrayModelval;
return "hsl( $col,100%,60%)";
break;
case 7: //800-850
$col=$ArrayModelval;
return "hsl( $col,100%,50%)";
break;
case 8: //850-900;
$col=$ArrayModelval;
return "hsl( $col,100%,50%)";
break;
case 9: //900-950
$col=$ArrayModelval;
return "hsl( $col,100%,40%)";
break;
case 10: //950-1000
$col=$ArrayModelval;
return "hsl($col,100%,40%)";
break;
}
}
For the most part does what I want: I want to color parts of the random seqeunce where there is a $hit - defined on external file. My only problem is when there is any overlap, i.e. if two hits are within 3 bases of each other the span is elongated and colored as if its one span.
The external file has a position to start a span which have variable colors depending on a score given in the external file. Basically if I have 3 results, 2 of which have almost the same hit (+-1) and the other a different hit, I would only see two sections colored, can anyone see what my problem is? Sorry I know I probably worded this horribly but its hard to explain. Thanks.
>chr1:2198584545754_genome_1000+ 500 1000 Model1 0
>chr2:2198581212154_genome_1000+ 510 992 Model2 180
>chr3:2115151215754_genome_1000+ 520 990 Model3 330
>chr4:2198584545754_genome_1000+ 530 980 Model3 330
>chr5:1218455145754_genome_1000+ 540 970 Model2 180
>chr6:1231354645454_genome_1000+ 550 960 Model1 0
>chr7:1231213211134_genome_1000+ 600 950 Model3 330
>chr7:1231213211134_genome_1000+ 650 940 Model3 330
javascript:
function toggle() {
var div= document.getElementById('coltext');
var modName=this.getAttribute('ModelName');
var spans=div.getElementsByTagName('span');
var spans_l=spans.length;
while (spans_l--){
span=spans[spans_l];
if(span.getAttribute('class')==modName && this.checked==true){
var color= span.getAttribute('color');
span.style.background=color;
}
if(span.getAttribute('class')==modName && this.checked==false){
span.style.background="white";
}
}
}
Try this on for size. It works by using a FIFO stack $currentHits to handle the hit boundaries. I have also added a few helper functions to deal with color generation - if you alter your getcolor() function to return an array instead of a CSS string one of them could be dropped, making it more efficient.
I have been unable to test this because I don't have the source code for your getcolor() or rawtransform() functions - if you add these to the question I am certain further improvements can be made and I can test the code properly. Also, it would be good to see what your CSS Model1, Model2 etc classes look like.
Edit: now includes getcolor()/rawtransform() functions and (at least partially) tested
<?php
function rawtransform ($raw) {
return (int) ($raw / 50) - 9;
}
function getcolor ($score, $h) {
switch ($score) {
// working. test each color.
case 1: /*500-550(Raw Score)*/
$l = 90;
break;
case 2: case 3: //550-650
$l = 85;
break;
case 4: //650-700
$l = 80;
break;
case 5: //700-750
$l = 70;
break;
case 6: //750-800
$l = 60;
break;
case 7: case 8: //800-900;
$l = 50;
break;
case 9: case 10: default: //900-1000 / out of range
$l = 40;
break;
}
return array(
'h' => $h,
's' => 100,
'l' => $l
);
}
function hsl_average_color () {
// Takes an unlimited number of arguments, calculates the average HSL value and returns a CSS string
$args = func_get_args();
$h = $s = $l = array();
foreach ($args as $arg) {
$h[] = $arg['h'];
$s[] = $arg['s'];
$l[] = $arg['l'];
}
return sprintf('hsl(%d, %d%%, %d%%)', (int) round(array_sum($h) / count($h)), (int) round(array_sum($s) / count($s)), round(array_sum($l) / count($l)));
}
$fileName = 'res_example.txt';
// Open the file
if (!$fp = fopen($fileName, 'r')) {
// Handle file read errors here
die("Unable to open file $fileName");
}
// Loop the file data and build an associative array
$resArr = array();
while (($line = fgetcsv($fp, 0, "\t")) !== FALSE) {
// You didn't declare $allModels as an empty array before the loop
// Should you have?
$allModels[$line[3]] = 1;
// Note that I have dropped the hit key and instead keyed the outer
// array by this value. I have added an end key to track the end of
// a hit
$resArr[$line[2]] = array(
'name' => $line[0],
'score' => $line[1],
'end' => $line[2] + 4,
'model' => $line[3],
'color' => getcolor(rawtransform($line[1]), $line[4])
);
}
// Close the file
fclose($fp);
// Generate a random sequence
$seqArr = array('A', 'T', 'C', 'G');
$randseq = array();
for ($i = 0; $i < 1000; $i++) {
$randseq[] = $seqArr[array_rand($seqArr)];
}
// $res appears to do nothing in you code
// $res = "";
// Open the <div>
echo '<div id="coltext" style="font-family:monospace;background-color:#000000;color:#FFFFFF;">'."\n";
// Iterate over $allModels and echo checkboxes
foreach ($allModels as $modName => $value) {
// ModelName is a non-standard HTML attribute, are you sure you meant to do this?
echo '<input ModelName="'.$modName.'" type="checkbox" checked="checked" onclick="toggle.apply(this);" />'.$modName."\n";
}
echo "<hr />\n";
// This line does nothing useful here
// $score = rawtransform($raw);
// An array to track the current hits
$currentHits = array();
foreach ($randseq as $index => $nuc) {
// Increment $index
$index++;
// Track whether we are in a hit/reached a boundary
$boundary = FALSE;
$inHit = (bool) count($currentHits);
// Check whether we are at the end of the lowest hit in the stack
if ($inHit && $index == $currentHits[0]['end']) {
$boundary = TRUE;
array_shift($currentHits);
}
// Check whether we are at the start of a new hit
if (isset($resArr[$index])) {
$boundary = TRUE;
$currentHits[] = $resArr[$index];
}
// If we reached a boundary
if ($boundary) {
// Close a hit
if ($inHit) {
echo "</span>";
}
// Open a hit
if (count($currentHits)) {
// Get the current color value
$colors = array();
foreach ($currentHits as $hit) $colors[] = $hit['color'];
$color = call_user_func_array('hsl_average_color', $colors);
// Start a new span
echo '<span class="'.$currentHits[0]['model'].'" title="position:'.$index.',score:'.$currentHits[0]['score'].'" style="color: '.$color.';">';
}
}
// Print the character
echo $nuc;
// Split into 50 character chunks
if (!($index % 50)){
echo"<br />\n";
}
}
// Close the last span if one is still open
if (count($currentHits)) {
echo "</span>";
}
// Close the <div>
echo "</div>\n";
[Disclaimer: I am new to PHP, and I am just learning, so please no flamers, it really hinders the learning process when one is trying to find solutions or information, thank you, and the code works fine in terms of the crossword puzzle, it's just really baffling me how one gets a diagonal orientation with the given information, or what I am doing wrong and not seeing?]
Given a switch:
switch ($dir){
case "E":
//col from 0 to board width - word width
//row from 0 to board height
$newCol = rand(0, $boardData["width"] - 1 - strlen($theWord));
$newRow = rand(0, $boardData["height"]-1);
for ($i = 0; $i < strlen($theWord); $i++){
//new character same row, initial column + $i
$boardLetter = $board[$newRow][$newCol + $i];
$wordLetter = substr($theWord, $i, 1);
//check for legal values in current space on board
if (($boardLetter == $wordLetter) ||
($boardLetter == ".")){
$board[$newRow][$newCol + $i] = $wordLetter;
} else {
$itWorked = FALSE;
} // end if
} // end for loop
break;
AND:
case "N":
//col from 0 to board width
//row from word length to board height
$newCol = rand(0, $boardData["width"] -1);
$newRow = rand(strlen($theWord), $boardData["height"]-1);
for ($i = 0; $i < strlen($theWord); $i++){
//check for a legal move
$boardLetter = $board[$newRow - $i][$newCol];
$wordLetter = substr($theWord, $i, 1);
if (($boardLetter == $wordLetter) ||
($boardLetter == ".")){
$board[$newRow - $i][$newCol] = $wordLetter;
} else {
$itWorked = FALSE;
} // end if
} // end for loop
break;
I should be able to combine the two to get NE (or diagonal text being outputted onto the screen)
HOWEVER, when I try this, it's not working, and I have been trying different combinations
of N and E to get NE or a diagonal NE orientation with no luck, what gives?
#Tried multiple different combination's of N & E, I am out of ideas
switch ($dir){
case "E":
$newCol = rand(0, $boardData["width"] - 1 - strlen($theWord));
$newRow = rand(strlen($theWord), $boardData["height"]-1);
for ($i = 0; $i < strlen($theWord); $i++){
#Combined but no luck, WTF:
$boardLetter = $board[$newRow][$newRow - $i];
$boardLetter = $board[$newCol][$newCol + $i];
$wordLetter = substr($theWord, $i, 1);
//check for legal values in current space on board
if (($boardLetter == $wordLetter) ||
($boardLetter == ".")){
$board[$newRow][$newRow - $i] = $wordLetter;
$board[$newCol][$newCol + $i] = $wordLetter;
} else {
$itWorked = FALSE;
} // end if
} // end for loop
break;
The break; statement will break out of the switch clause after running the code for case "E". You'll need to set up new, explicit cases for the combinations to make it work that way.
Suppose I have some serially numbered items that are 1-n units wide, that need to be displayed in rows. Each row is m units wide. I need some pseudo-code that will output the rows, for me, so that the m-width limit is kept. This is not a knapsack problem, as the items must remain in serial number order - empty spaces at the end of rows are fine.
I've been chasing my tail over this, partly because I need it in both PHP and jQuery/javascript, hence the request for pseudo-code....
while (!items.isEmpty()) {
rowRemain = m;
rowContents = [];
while (!items.isEmpty() && rowRemain > items[0].width) {
i = items.shift();
rowRemain -= i.width
rowContents.push(i);
}
rows.push(rowContents);
}
Running time is Θ(number of items)
Modulus is your friend. I would do something like:
$items = array(/* Your list of stuff */);
$count = 0;
$maxUnitsPerRow = 4; // Your "m" above
while ($item = $items[$count]) {
if ($count % $maxUnitsPerRow == 0) {
$row = new row();
}
$row->addItemToRow($item);
$count++;
}
Here is an alternative php code ...
function arrayMaxWidthString($items, $maxWidth) {
$out = array();
if (empty($items)) {
return $out;
}
$row = $maxWidth;
$i = 0;
$item = array_shift($items);
$row -= strlen($item);
$out[0] = $item;
foreach ($items as $item) {
$l = strlen($item);
$tmp = ($l + 1);
if ($row >= $tmp) {
$row -= $tmp;
$out[$i] = (($row !== $maxWidth) ? $out[$i] . ' ' : '') . $item;
} elseif ($row === $maxWidth) {
$out[$i] = $item;
++$i;
} else {
++$i;
$row = $maxWidth - $l;
$out[$i] = $item;
}
}
return $out;
}
For what it's worth, I think I have what I was looking for, for PHP - but not sure if there is a simpler way...
<?php
// working with just a simple array of widths...
$items = array(1,1,1,2,1,1,2,1);
$row_width = 0;
$max_width = 2;
echo "Begin\n"; // begin first row
foreach($items as $item=>$item_width) {
// can we add item_width to row without going over?
$row_width += $item_width;
if($row_width < $max_width) {
echo "$item_width ";
} else if($row_width == $max_width) {
echo "$item_width";
echo "\nEnd\nBegin\n"; // end last row, begin new row
$row_width = 0;
} else if($row_width == 2* $max_width) {
echo "\nEnd\nBegin\n"; // end last row, begin new row
echo "$item_width";
echo "\nEnd\n"; // end new row
$row_width = 0;
if($item < count($items)) echo "Begin\n"; // new row
} else if($row_width > $max_width) {
echo "\nEnd\nBegin\n"; // end last row, begin new row
echo "$item_width";
$row_width = $item_width;
}
}
echo "\nEnd\n"; // end last row
?>