Define a variable in a function php [duplicate] - php

This question already has an answer here:
Notice : Undefined variable when concatenating
(1 answer)
Closed 7 years ago.
I have a function as below but I get warning for $tab variable that says it is not defined. How can I define it and not receive this warning anymore?
<?php
/*** suggested articles as random ***/
function doArticle_suggested_small_horizontall($articleid,$title,$photo,$parentid,$catid,$altdescription) {
$tab .= "<table width=150 cellspacing=5 style=border: 1px solid #0066ff align=right>\n";
$tab .= "<tr>\n";
$tab .= "<td align=center bgcolor=#ffffff><a href='../artandculture/adetails.php?articleid=$articleid&parentid=$parentid&catid=$catid'>
<img src='../images/simage/$photo' border='0' alt='$altdescription'></a></td>\n";
$tab .= "</tr>\n";
$tab .= "<tr align=right width=150 height=80 border=0 style=border: 1px solid #ffffff>\n";
$tab .= "<td width=110 align=right dir='rtl' border=0 style=border: 1px solid #ffffff ><p class=articletitlenounderline><a href='../artandculture/adetails.php?articleid=$articleid&parentid=$parentid&catid=$catid'><strong>$title </strong></p></a></td>\n";
$tab .= "</tr>\n";
$tab .= "</table> <p> <hr class='hr99' ></hr></p>";
return $tab;
}
$tab = "";
?>

You need to define $tab at the beginning of the function.
replace $tab .= "<table width=15 ....
with $tab = "<table width=15....
Or you could just add $tab = ""; as the first line of the function, now you are defining it outside the function which you should remove.

Define $tab,
$tab = [...];
Then you need to do .= on the variable. The variable does not exist where you start using it.

".=" is telling PHP to add a string to an existing variable. PHP cannot add the string if there is no existing variable.
// Add this before your first $tab .=
$tab = NULL;

Move $tab to the start of the function in order for the concatenation to properly work:
Instead of this in the end:
$tab = "";
Do this:
<?php
/////////////////////suggested articles as random /////////////////////////
function doArticle_suggested_small_horizontall($articleid,$title,$photo,$parentid,$catid,$altdescription) {
$tab = "";
$tab .= "<table width=150 cellspacing=5 style=border: 1px solid #0066ff align=right>\n";
$tab .= "<tr>\n";
$tab .= "<td align=center bgcolor=#ffffff><a href='../artandculture/adetails.php?articleid=$articleid&parentid=$parentid&catid=$catid'>
<img src='../images/simage/$photo' border='0' alt='$altdescription'></a></td>\n";
$tab .= "</tr>\n";
$tab .= "<tr align=right width=150 height=80 border=0 style=border: 1px solid #ffffff>\n";
$tab .= "<td width=110 align=right dir='rtl' border=0 style=border: 1px solid #ffffff ><p class=articletitlenounderline><a href='../artandculture/adetails.php?articleid=$articleid&parentid=$parentid&catid=$catid'><strong>$title </strong></p></a></td>\n";
$tab .= "</tr>\n";
$tab .= "</table> <p> <hr class='hr99' ></hr></p>";
return $tab;
}
?>

In addition to the other answers, if you already have something assigned to $tab and want to append it from function, you must either send it in as an argument to the function or add the function output
function example( $tab, $something = false ) {
$tab .= ', added this in example function';
return $tab;
}
$tab = 'Original content';
echo example( $tab ); // Output: Original content, added this in example function
function example2( $something = false ) {
return ', added this in example2 function';
}
$tab = 'Original content 2';
$tab .= example2();
echo $tab: // Output: Original content 2, added this in example2 function

Related

How to change the cell color of HTML table according to a threshold

I have a mysql query as below in my php page,
$query = "
select table_date as Date,vals as Value from usertable.table1 group by table_date;";
I am fetching data to columns from below method.
$result=mysqli_query($conn,$query);
while ($row = $result -> fetch_assoc())
{
$q2Date .= '<td style="text-align:center"><b>'.$row['Date'].'</td>';
$q2Value .= '<td style="text-align:center">'.$row['Value'].'</td>';
$row['Date']++;
}
Then I am using $body variable as below to show them in a html table.
$body.= '
<table style="top: 15px; left:10px; " border=1>
<tbody>
<caption style="color: rgb(241, 239, 243);background: rgb(1, 32, 65); ">Total Value</caption>
<tr>
<td style="text-align:center"><b>Date</td>'.$q2Date .'
</tr>
<tr>
<td style="text-align:center"><b>Value</td>'.$q2Value .'
</tr>
</tbody>
</table>
';
From above method I could get the output as below,
Now I need to check each cell and change its color if it differs more than 25% from average value(lets say 250). Then the output should show as follows,
What changes to be done in my original code? Do I need to change my query as well? or HTML or both?
check if value is greater than 250 then set background color style:
<?php
$result=mysqli_query($conn,$query);
while ($row = $result -> fetch_assoc())
{
$q2Date .= '<td style="text-align:center"><b>'.$row['Date'].'</td>';
if($row['Value'] >= 250)
$q2Value .= '<td style="text-align:center;background-color:red;">'.$row['Value'].'</td>';
else
$q2Value .= '<td style="text-align:center;">'.$row['Value'].'</td>';
$row['Date']++;
}
its so simple to create a class in CSS let's say lessCell
.lessCell{
background:red;
}
then check the value if it is true add the class to if it's not do nothing
$cls="";
$result=mysqli_query($conn,$query);
while ($row = $result -> fetch_assoc())
{
if(your_condition==true){
$cls='class="lessCell"';
}else{
$cls=""
}
$q2Date .= '<td '.$cls.' style="text-align:center"><b>'.$row['Date'].'</td>';
$q2Value .= '<td style="text-align:center">'.$row['Value'].'</td>';
$row['Date']++;
}

Have dynamic content span across pages in php

I have the following code that dynamically loads items in an invoice. I am looking for a way to adds excess items to a new page, and so on. I would like to limit the # of items on a page to a set amount, say 15 items. Is there a way to do this in php? The code below is within a document that uses dompdf to appear in pages form
foreach ( $invoice['InvoiceDetail'] as $key=>$item){
$html .= '<tr style="border-bottom: 1px solid #ccc; line-height: 15px;">';
$itemNo = isset($item['product_id']) ? $item['product_id'] : '';
$itemName = isset($item['productName']) ? $item['productName']: '';
$price = isset($item['price']) ? invoiceNumFormat($item['price']): '';
$quantity = isset($item['quantity']) ? $item['quantity'] : '';
$total = invoiceNumFormat( $item['price']*$item['quantity']);
$html .= '<td style="text-align: left"><h5>'.$itemNo.'</h5></td>';
$html .= '<td style="text-align: left">'.$itemName.'</td>';
$html .= '<td style="text-align: left">'.$price.'</td>';
$html .= '<td style="text-align: left" width="10px">'.$quantity.'</td>';
$html .= '<td style="text-align: right">'.$total.'</td>';
$html .= '</tr>';
}
For your particular situation you could just indicate to dompdf to break the page, something along the lines of:
$itemCount = 0;
foreach ( $invoice['InvoiceDetail'] as $key=>$item){
$itemCount++;
$html .= '<tr style="border-bottom: 1px solid #ccc; line-height: 15px;">';
/* snip */
if ($itemCount % 20 == 0) {
$html .= '<tr><td><div style="page-break-before: always;"></div></td></tr>';
}
$html .= '<td style="text-align: left"><h5>'.$itemNo.'</h5></td>';
$html .= '<td style="text-align: left">'.$itemName.'</td>';
$html .= '<td style="text-align: left">'.$price.'</td>';
$html .= '<td style="text-align: left" width="10px">'.$quantity.'</td>';
$html .= '<td style="text-align: right">'.$total.'</td>';
$html .= '</tr>';
}
Dompdf does not yet recognize page break styling on table rows/cells. Otherwise it would make more sense to place the styling there.
Does this help?
<?php
$per_page = 20;
if ( $pagenum < 2 || !$pagenum ) {
$limit_start = 0;
} else {
$limit_start = (( $pagenum -1 ) * $per_page);
}
$sql = "select foo, bar, baz from table where $conditions limit $limit_start, $per_page";
$db->query( $sql ); //or whatever your method is
// while statement to get data into $invoice array
//foreach statement to display it
//pagination links

Parse error: syntax error, unexpected '<' in C:\xampp\htdocs\jmb_system\anggerik\viewresident.php on line 44

I'm having this error coming out on this script about view data script.
Please help! does i lack of " which is the last third line of the code below.
function ListUnit()
{
?>
<table class="hilite" id="highlight"><thead>
<tr><th>No.</th><th>Owner</th><th>Unit</th><th>Block</th><th>Floor</th><th>Number</th><th>Balance</th><th>Additional Info</th><th>Action</th></tr>
</thead>
<tbody>
<?php
$SQL="SELECT * FROM unit ORDER BY block, floor, unit ASC";
$result=mysql_query($SQL);
$i=1;
while ($row=mysql_fetch_assoc($result))
{
$id=$row['id'];
$ownerName=$row['ownerName'];
$ownerHP=$row['ownerHP'];
$name=$row['unitname'];
$block=$row['block'];
$floor=$row['floor'];
$unitnum=$row['unit'];
$sqft=$row['sqft'];
$resitsyabas=$row['resit_syabas'];
$ltsb=$row['LTSB'];
$ppaa=$row['PPAA'];
$b482=$row['b482'];
$tbs=$row['TBS'];
$addinfo=$row['addinfo'];
$addinfo = wordwrap($addinfo, 30, "<br>", true);
echo "<tr align=\"center\" valign=\"top\">";
echo "<td>$i</td><td align=\"left\" valign=\"top\">Name:$ownerName<br>Contact:$ownerHP</td><td>$name</td><td>$block</td><td>$floor</td><td>$unitnum</td>
<td align=\"left\">LTSB:RM$ltsb<br>PPAA:RM$ppaa<br>482:RM$b482<br>TBS:$tbs</td>
<td align=\"left\">Area:$sqft sqft<br>Meter Syabas:$resitsyabas<br>$addinfo</td><td>Edit</td>";
echo "</tr>";
$i++;
}
</tbody></table>
}
?>
so, kindly consult. how and where should i fix it?
You can not use HTML tag in a function.
You must be use
function ListUnit()
{
$html = '<table class="hilite" id="highlight"><thead>';
$html .= '<tr><th>No.</th><th>Owner</th><th>Unit</th><th>Block</th><th>Floor</th><th>Number</th><th>Balance</th><th>Additional Info</th><th>Action</th></tr>';
$html .= '</thead>';
$html .= '<tbody>';
$SQL="SELECT * FROM unit ORDER BY block, floor, unit ASC";
$result=mysql_query($SQL);
$i=1;
while ($row=mysql_fetch_assoc($result))
{
$id=$row['id'];
$ownerName=$row['ownerName'];
$ownerHP=$row['ownerHP'];
$name=$row['unitname'];
$block=$row['block'];
$floor=$row['floor'];
$unitnum=$row['unit'];
$sqft=$row['sqft'];
$resitsyabas=$row['resit_syabas'];
$ltsb=$row['LTSB'];
$ppaa=$row['PPAA'];
$b482=$row['b482'];
$tbs=$row['TBS'];
$addinfo=$row['addinfo'];
$addinfo = wordwrap($addinfo, 30, "<br>", true);
$html .= "<tr align=\"center\" valign=\"top\">";
$html .= "<td>$i</td><td align=\"left\" valign=\"top\">Name:$ownerName<br>Contact:$ownerHP</td><td>$name</td><td>$block</td><td>$floor</td><td>$unitnum</td><td align=\"left\">LTSB:RM$ltsb<br>PPAA:RM$ppaa<br>482:RM$b482<br>TBS:$tbs</td><td align=\"left\">Area:$sqft sqft<br>Meter Syabas:$resitsyabas<br>$addinfo</td><td>Edit</td>";
$html .= "</tr>";
$i++;
}
$html .= "</tbody></table>";
echo $html;
}
?>
Try replacing single quote with double while printing, something like below:
echo "<td>$i</td><td align='left' valign='top'>Name:$ownerName<br>Contact:$ownerHP</td><td>$name</td><td>$block</td><td>$floor</td><td>$unitnum</td>
<td align='left'>LTSB:RM$ltsb<br>PPAA:RM$ppaa<br>482:RM$b482<br>TBS:$tbs</td>
<td align='left'>Area:$sqft sqft<br>Meter Syabas:$resitsyabas<br>$addinfo</td><td><a href='admin_unit.php?a=1&i=$id'>Edit</a></td>";
One more point, you are missing echo before :
$html .= "</tbody></table>";

How to embed icons inside an HTML table obtained from a csv?

The php code below extracts the data from a .csv file and generates an HTML table. It works fine. I wonder if it is possible to embed in the html table some icons, such as a red or green dot (reddot.gif , greendot.gif) in correspondence of some specific words (example: red , green) in the csv file.
In other words, when in the csv file appear red or green in a specific column (example: column 3), should appear the reddot.gif or greendot.gif in the generated html file.
Thanks in advance. Mat
<?php
$row = 1;
if (($handle = fopen("example.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
if ($row == 1) {
echo '<tr>';
}else{
echo '<tr>';
}
for ($c=0; $c < $num; $c++) {
if(empty($data[$c])) {
$value = " ";
}else{
$value = $data[$c];
}
if ($row == 1) {
// ------------- head row --------
echo '<td style="border-top: 1px solid rgb(111,180,224); border-left: 1px solid rgb(111,180,224); border-bottom: 1px solid rgb(111,180,224);" align="left" bgcolor="#0066cc" height="36" valign="middle" ><b><font color="#ffffff" size="2"> '.$value.' </font></b></td>';
}else{
// ------------- Generic row -------
echo '<td style=" border-bottom: 1px solid rgb(111,180,224);" sdval="9" sdnum="1040;" align="left" bgcolor="#ffffff" height="25" valign="middle"><font color="#000000" size="2"> '.$value.' </font></td>';
}
}
if ($row == 1) {
echo '</tr>';
}else{
echo '</tr>';
}
$row++;
}
echo '</tbody></table>';
echo '</center>';
fclose($handle);
}
?>
declare this function outside of any loops, just at the beginning of the if, or even at the beginning or ending of the script:
function img($img){ return "<img src='{$img}dot.gif'/>";}
this goes in the else:
$value = preg_replace(array('/red/i', '/green/i'), array(img('red'), img('green')), $data[$c]);
If you need more colors just follow the logic and add them in the two arrays inside of the preg_replace.
If you need to add attributes to the images add them in the function declared.
Hope it works for you
Please give this a try.
if(empty($data[$c])) {
$value = " ";
}else{
$value = $data[$c];
switch(strtolower(trim($value))){
case 'green': $value = '<img src="greendot.gif" '
.'alt="green" height="32" width="32">';
break;
case 'red': $value = '<img src="reddot.gif" '
.'alt="red" height="32" width="32">';
break;
// you can add other cases here like blue, triangle etc :)
}
}

Using jquery to hide and show php dynamicly created content "Search Results"

I have a table that displays my search results, and pending if a user has a function open I want to only display 5 results per row, and the same if they have it closed I want to show 7 results. This is working fine, but the issue I am having is when I toggle the function the div is hidden or shows but the PHP content that was dynamically created is still visible. I know that the div hides or shows pending the function because I wrote static text in the dynamically create divs and pending the function the text is shown or hidden.
Question: How to hide the php content as well?
PHP content =
/*BIG*/
//begin new row
if($result_incBig == $beginRowBig){
$b .= '<tr>';
//row color
$row_color = ($color_incBig % 2) ? 'tdAltRow_A' : 'tdAltRow_B';
if($i == ($num_qk-1)){
$secondRowClass = 'tdAltRow_noBorder';
}else{
$secondRowClass = 'tdAltRow';
}
//increment the color of the row
++$color_incBig;
}
$b .= '<td class="td_formTitle_noBorder" align="center" style="padding:0px; text-indent: 0;" width="165" height="115" valign="bottom">';
$b .= '<table width="100%" cellpadding="0" cellspacing="0" border="0">';
$b .= '<tr>';
$b .= '<td align="center" style="height:80px; padding:4px; vertical-align:middle;">'.$appendLinkBegin.$companyPic.$appendLinkEnd.'</td>';
$b .= '</tr>';
$b .= '<tr>';
$b .= '<td align="center" style="height:35px; padding:2px; vertical-align:bottom;">'.$appendLinkBegin.'<b>'.$company_name.'</b>'.$addSpace.'<br />'.$num2.' '.$resultText.$appendLinkEnd.'</td>';
$b .= '</tr>';
$b .= '</table>';
$b .= '</td>';
//end row if out of results or reached max num for row
if($result_incBig == $endRowBig){
$b .= '</tr>';
$result_incBig = 0;
}else if(($i+1) == $num_qk){
if($result_incBig < $endRowBig){
for($e=0;$e<=($endRowBig-$result_incBig);++$e){
$b .= '<td class="td_formTitle_noBorder" align="center" style="padding:0px; text-indent: 0;" width="165" height="115" valign="bottom"><!--space--></td>';
}
$b .= '</tr>';
$result_incBig = 0;
}
}
//increment inc
++$result_incBig;
/*END BIG*/
/*SMALL*/
//begin new row
if($result_incSmall == $beginRowSmall){
$s .= '<tr>';
//row color
$row_color = ($color_incSmall % 2) ? 'tdAltRow_A' : 'tdAltRow_B';
if($i == ($num_qk-1)){
$secondRowClass = 'tdAltRow_noBorder';
}else{
$secondRowClass = 'tdAltRow';
}
//increment the color of the row
++$color_incSmall;
}
$s .= '<td class="td_formTitle_noBorder" align="center" style="padding:0px; text-indent: 0;" width="165" height="115" valign="bottom">';
$s .= '<table width="100%" cellpadding="0" cellspacing="0" border="0">';
$s .= '<tr>';
$s .= '<td align="center" style="height:80px; padding:4px; vertical-align:middle;">'.$appendLinkBegin.$companyPic.$appendLinkEnd.'</td>';
$s .= '</tr>';
$s .= '<tr>';
$s .= '<td align="center" style="height:35px; padding:2px; vertical-align:bottom;">'.$appendLinkBegin.'<b>'.$company_name.'</b>'.$addSpace.'<br />'.$num2.' '.$resultText.$appendLinkEnd.'</td>';
$s .= '</tr>';
$s .= '</table>';
$s .= '</td>';
//end row if out of results or reached max num for row
if($result_incSmall == $endRowSmall){
$s .= '</tr>';
$result_incSmall = 0;
}else if(($i+1) == $num_qk){
if($result_incSmall < $endRowSmall){
for($e=0;$e<=($endRowSmall-$result_incSmall);++$e){
$s .= '<td class="td_formTitle_noBorder" align="center" style="padding:0px; text-indent: 0;" width="165" height="115" valign="bottom"><!--space--></td>';
}
$s .= '</tr>';
$result_incSmall = 0;
}
}
//increment inc
++$result_incSmall;
/*END SMALL*/
}
//display table
if($where == ''){
$b = '<input type="hidden" name="numberRes" id="numberRes" value="No Results" />';
$s = '<input type="hidden" name="numberRes" id="numberRes" value="No Results" />';
}
echo '<table cellpadding="0" cellspacing="0" border="0" width="100%">';
//results
echo '<div class="rowFillBig">big7Results'.$b.'</div>';
echo '<div class="rowFillSmall" style="display:none;">small5Results'.$s.'</div>';
echo '</table>';
The Jquery function:
$(function () {
$('#tab_1').click(function(){
$('#tab_vid').toggle();
if ($("#mainContentRF").width() == 825){
$("#mainContentRF").width(1180);
$("audio").width(800);
//if viewing the full screen pause video
<?php if($viewingVideo > ''){ ?>
video = $('.videoplayer').get(0);
video.pause();
<?php } ?>
<!--hide small-->
$(".rowFillSmall").css("display", "none");
<!--show big-->
$(".rowFillBig").css("display", "inline");
}else{
$("#mainContentRF").width(825);
$("audio").width(500);
<!--show small-->
$(".rowFillSmall").css("display", "inline");
<!--hide big-->
$(".rowFillBig").css("display", "none");
}
<!--if tab was left open dont close it-->
<?php if($acc_openValue_1 == '99'){ ?>
})
<?php }else{ ?>
}).click();
<?php } ?>
});
If I can be more clear please let me know. Basically the div and any content inside should hide when asked to, and show when asked.
Place the div's on the outside of the table like this.
echo '<div class="rowFillBig">';
echo '<table cellpadding="0" cellspacing="0" border="0" width="100%">';
//results
echo $b;
echo '</table>';
echo '</div>';
echo '<div class="rowFillSmall" style="display:none;">';
echo '<table cellpadding="0" cellspacing="0" border="0" width="100%">';
//results
echo $s;
echo '</table>';
echo '</div>';

Categories