lingo5 Posted May 25, 2011 Share Posted May 25, 2011 Hello, I'm fairly new to PHP/MySQL. I am trying to print some text in 2 columns and this is the code I'm using $colname_hometext_RS = "-1"; if (isset($_GET['home'])) { $colname_hometext_RS = $_GET['home']; } mysql_select_db($database_MySQLconnect, $MySQLconnect); $query_hometext_RS = sprintf("SELECT * FROM t_textos WHERE id_texto = %s", GetSQLValueString($colname_hometext_RS, "int")); $hometext_RS = mysql_query($query_hometext_RS, $MySQLconnect) or die(mysql_error()); $row_hometext_RS = mysql_fetch_assoc($hometext_RS); $totalRows_hometext_RS = mysql_num_rows($hometext_RS); ?> <? function split_pos($row_hometext_RS['texto_esp']) { /* find middle space in text */ $mid = (int) strlen($row_hometext_RS['texto_esp'])/2 – 1; $cut = strpos($row_hometext_RS['texto_esp'] , ‘ ‘ , $mid); $part1= substr($row_hometext_RS['texto_esp'] , 0 , $cut + 1); $pos1 = strrpos($part1 , ‘<’); $pos2 = strrpos($part1 , ‘>’); if (($pos1 < $pos2) or ($pos1 === False)) { return $cut; /* no html tag around */ } $pos3 = strpos($row_hometext_RS['texto_esp'] , ‘>’ , $cut1 + 1); if ($pos3 !== False) { return $pos3; /* end of middle html tag */ } else { return $cut; /* unbalancing < > */ } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> <style type="text/css"> <!-- body { margin-top: 0px; } --> </style> <link href="styles/cantera_styles.css" rel="stylesheet" type="text/css" /> </head> <body> <?php $middle_pos = split_pos($row_hometext_RS['texto_esp']); Print “<table><tr>”; // First column Print “<td>” . substr($row_hometext_RS['texto_esp'], 0, $middle_pos) . “</td>; Print “<td style=’width:30px’></td>”; // Second column Print “<td>” . substr($row_hometext_RS['texto_esp'], $middle_pos + 1) . “</td>”; Print “</tr></table>”; ?> <?php require_once('footer.php'); ?> <?php mysql_free_result($hometext_RS); ?> </body> </html> ...but nothing comes out....just a blank page. Please help. Quote Link to comment https://forums.phpfreaks.com/topic/237423-please-help-with-displaying-text-in-columns/ Share on other sites More sharing options...
dougjohnson Posted May 25, 2011 Share Posted May 25, 2011 You had lot's of syntax errors. I think I fixed most of them but there could be more... <?php $colname_hometext_RS = "-1"; if (isset($_GET['home'])) { $colname_hometext_RS = $_GET['home']; } mysql_select_db($database_MySQLconnect, $MySQLconnect); $query_hometext_RS = sprintf("SELECT * FROM t_textos WHERE id_texto = %s", GetSQLValueString($colname_hometext_RS, "int")); $hometext_RS = mysql_query($query_hometext_RS, $MySQLconnect) or die(mysql_error()); $row_hometext_RS = mysql_fetch_assoc($hometext_RS); $totalRows_hometext_RS = mysql_num_rows($hometext_RS); function split_pos($row_hometext_RS['texto_esp']) { /* find middle space in text */ $mid = (int) strlen($row_hometext_RS['texto_esp'])/2 – 1; $cut = strpos($row_hometext_RS['texto_esp'] , " " , $mid); $part1= substr($row_hometext_RS['texto_esp'] , 0 , $cut + 1); $pos1 = strrpos($part1 , "<"); $pos2 = strrpos($part1 , ">"); if (($pos1 < $pos2) || ($pos1 === False)) { return $cut; /* no html tag around */ } $pos3 = strpos($row_hometext_RS['texto_esp'] , ">" , $cut1 + 1); if ($pos3 !== False) { return $pos3; /* end of middle html tag */ } else { return $cut; /* unbalancing < > */ } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> <style type="text/css"> <!-- body { margin-top: 0px; } --> </style> <link href="styles/cantera_styles.css" rel="stylesheet" type="text/css" /> </head> <body> <?php $middle_pos = split_pos($row_hometext_RS['texto_esp']); echo "<table><tr>"; // First column echo "<td>" . substr($row_hometext_RS['texto_esp'], 0, $middle_pos) . "</td>"; echo "<td style='width:30px'></td>"; // Second column echo "<td>" . substr($row_hometext_RS['texto_esp'], $middle_pos + 1) . "</td>"; echo "</tr></table>"; require_once('footer.php'); mysql_free_result($hometext_RS); ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/237423-please-help-with-displaying-text-in-columns/#findComment-1220100 Share on other sites More sharing options...
lingo5 Posted May 25, 2011 Author Share Posted May 25, 2011 many thanks dougjohnson, but I still get thie error Parse error: parse error, expecting `')'' in C:\wamp\www\airgom\index.php on line 44 <?php require_once('Connections/MySQLconnect.php'); ?> <?php function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue; switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . date("Y-d-m",strtotime($theValue)) . "'" : "NULL"; break; case "time": $theValue = ($theValue != "") ? "'" . date("H:i:s",strtotime($theValue)) . "'" : "NULL"; break; case "datetime": $theValue = ($theValue != "") ? "'" . date("Y-d-m H:i:s",strtotime($theValue)) . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } $colname_hometext_RS = "-1"; if (isset($_GET['home'])) { $colname_hometext_RS = $_GET['home']; } mysql_select_db($database_MySQLconnect, $MySQLconnect); $query_hometext_RS = sprintf("SELECT * FROM t_textos WHERE id_texto = %s", GetSQLValueString($colname_hometext_RS, "int")); $hometext_RS = mysql_query($query_hometext_RS, $MySQLconnect) or die(mysql_error()); $row_hometext_RS = mysql_fetch_assoc($hometext_RS); $totalRows_hometext_RS = mysql_num_rows($hometext_RS); function split_pos($row_hometext_RS['texto_esp']) { /* find middle space in text */ $mid = (int) strlen($row_hometext_RS['texto_esp'])/2 – 1; $cut = strpos($row_hometext_RS['texto_esp'] , " " , $mid); $part1= substr($row_hometext_RS['texto_esp'] , 0 , $cut + 1); $pos1 = strrpos($part1 , "<"); $pos2 = strrpos($part1 , ">"); if (($pos1 < $pos2) || ($pos1 === False)) { return $cut; /* no html tag around */ } $pos3 = strpos($row_hometext_RS['texto_esp'] , ">" , $cut1 + 1); if ($pos3 !== False) { return $pos3; /* end of middle html tag */ } else { return $cut; /* unbalancing < > */ } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> <style type="text/css"> <!-- body { margin-top: 0px; } --> </style> <link href="styles/cantera_styles.css" rel="stylesheet" type="text/css" /> </head> <body> <?php $middle_pos = split_pos($row_hometext_RS['texto_esp']); echo "<table><tr>"; // First column echo "<td>" . substr($row_hometext_RS['texto_esp'], 0, $middle_pos) . "</td>"; echo "<td style='width:30px'></td>"; // Second column echo "<td>" . substr($row_hometext_RS['texto_esp'], $middle_pos + 1) . "</td>"; echo "</tr></table>"; require_once('footer.php'); mysql_free_result($hometext_RS); ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/237423-please-help-with-displaying-text-in-columns/#findComment-1220105 Share on other sites More sharing options...
Drummin Posted May 25, 2011 Share Posted May 25, 2011 OR a more simple approach. <?php function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue; switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . date("Y-d-m",strtotime($theValue)) . "'" : "NULL"; break; case "time": $theValue = ($theValue != "") ? "'" . date("H:i:s",strtotime($theValue)) . "'" : "NULL"; break; case "datetime": $theValue = ($theValue != "") ? "'" . date("Y-d-m H:i:s",strtotime($theValue)) . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } $colname_hometext_RS = "-1"; if (isset($_GET['home'])) { $colname_hometext_RS = $_GET['home']; } mysql_select_db($database_MySQLconnect, $MySQLconnect); $query_hometext_RS = sprintf("SELECT * FROM t_textos WHERE id_texto = %s", GetSQLValueString($colname_hometext_RS, "int")); $hometext_RS = mysql_query($query_hometext_RS, $MySQLconnect) or die(mysql_error()); $row_hometext_RS = mysql_fetch_assoc($hometext_RS); $totalRows_hometext_RS = mysql_num_rows($hometext_RS); $textoesp=$row_hometext_RS['texto_esp']; $charcount = strlen($textoesp); $mid = number_format($charcount/2, 0, '.', ''); $halflast = $mid-1; $cut = strpos($textoesp , " " , $halflast); $part1= substr($textoesp , 0 , $cut); $part2= substr($textoesp , $cut , $charcount); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> <style type="text/css"> <!-- body { margin-top: 0px; } --> </style> <link href="styles/cantera_styles.css" rel="stylesheet" type="text/css" /> </head> <body> <?php echo "<table><tr>"; // First column echo "<td>$part1</td>"; echo "<td style='width:30px'></td>"; // Second column echo "<td>$part2</td>"; echo "</tr></table>"; require_once('footer.php'); mysql_free_result($hometext_RS); ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/237423-please-help-with-displaying-text-in-columns/#findComment-1220154 Share on other sites More sharing options...
lingo5 Posted May 25, 2011 Author Share Posted May 25, 2011 Thnaks drummin, but I get the following error Warning: strpos() [function.strpos]: Offset not contained in string in C:\wamp\www\airgom\index.php on line 49 any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/237423-please-help-with-displaying-text-in-columns/#findComment-1220157 Share on other sites More sharing options...
lingo5 Posted May 25, 2011 Author Share Posted May 25, 2011 This is line 49 by the way $cut = strpos($textoesp , " " , $halflast); Quote Link to comment https://forums.phpfreaks.com/topic/237423-please-help-with-displaying-text-in-columns/#findComment-1220158 Share on other sites More sharing options...
xyph Posted May 25, 2011 Share Posted May 25, 2011 I use a function like this to move data into columns. <?php // $data - array of data // $num - how many columns // [$cellStart] - what to put at the start of a new cell // [$cellEnd] - what to put at the end of a new cell // [$rowStart] - what to put at the start of a new row // [$rowEnd] - what to put at the end of a new row // [$fill] - what to fill left over cells at the end with function to_columns( $data, $num, $cellStart='', $cellEnd='', $rowStart='', $rowEnd='', $fill='' ) { if( !is_array($data) || empty($data) || $num < 0 ) return FALSE; $return = ''; $col = 0; foreach( $data as $value ) { if ( $col == 0 ) $return .= $rowStart; $return .= $cellStart . $value . $cellEnd; $col++; if( $col == $num ) { $col = 0; $return .= $rowEnd; } } for( ;$col<$num;$col++ ) $return .= $fill; return $return; } $data = array( 'aaa','bbb','ccc','ddd','eee','fff','ggg','hhh','iii','jjj', 'kkk','lll','mmm','nnn','ooo','ppp','qqq','rrr','sss','ttt','uuu','vvv','www', 'xxx','yyy','zzz' ); echo "<table border=\"1\">\n" . to_columns( $data, 4, " <td>", "</td>\n", " <tr>\n", " </tr>\n", " <td> </td>\n" ) . "</table>\n"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/237423-please-help-with-displaying-text-in-columns/#findComment-1220162 Share on other sites More sharing options...
lingo5 Posted May 25, 2011 Author Share Posted May 25, 2011 thanks symph, but what i'm trying to do is to distribute a big chunk of text in 2 columns. Quote Link to comment https://forums.phpfreaks.com/topic/237423-please-help-with-displaying-text-in-columns/#findComment-1220168 Share on other sites More sharing options...
Drummin Posted May 25, 2011 Share Posted May 25, 2011 Forgive me for butchering your code. I'm not Sure about GetSQLValueString(). I believe it is a Dreamweaver custom-built function and not part of php. I will change your code to make the query using an id grabbed from $_GET['home']. Again, this code works fine for me when I use my own table query but when using GetSQLValueString() I get nothing. This is why I made the change. Take it or leave it. <?php //NOT Sure about GetSQLValueString(). I believe it is a Dreamweaver custom-built function and not part of php. I will change your query// //******************************************************************// //function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") //{ // $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue; // switch ($theType) { // case "text": // $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; // break; // case "long": // case "int": // $theValue = ($theValue != "") ? intval($theValue) : "NULL"; // break; // case "double": // $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL"; // break; // case "date": // $theValue = ($theValue != "") ? "'" . date("Y-d-m",strtotime($theValue)) . "'" : "NULL"; // break; // case "time": // $theValue = ($theValue != "") ? "'" . date("H:i:s",strtotime($theValue)) . "'" : "NULL"; // break; // case "datetime": // $theValue = ($theValue != "") ? "'" . date("Y-d-m H:i:s",strtotime($theValue)) . "'" : "NULL"; // break; // case "defined": // $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; // break; // } // return $theValue; //} //$colname_hometext_RS = "-1"; //if (isset($_GET['home'])) { // $colname_hometext_RS = $_GET['home']; //} mysql_select_db($database_MySQLconnect, $MySQLconnect); //NOT Sure about GetSQLValueString(). I believe it is a Dreamweaver custom-built function and not part of php. I will change your query// //******************************************************************// //$query_hometext_RS = sprintf("SELECT * FROM t_textos WHERE id_texto = %s", GetSQLValueString($colname_hometext_RS, "int")); //$hometext_RS = mysql_query($query_hometext_RS, $MySQLconnect); // or die(mysql_error()) //$row_hometext_RS = mysql_fetch_assoc($hometext_RS); //$totalRows_hometext_RS = mysql_num_rows($hometext_RS); //********************************************// ///I will assume you are using $_GET['home'] to grab an id number// //********************************************// if (isset($_GET['home'])) { $id = $_GET['home']; } $query_hometext_RS = mysql_query("SELECT * FROM t_textos WHERE id_texto = $id"); WHILE($hometext_RS = mysql_fetch_array($query_hometext_RS)) { $textoesp=$hometext_RS['texto_esp']; } //*********TEST*********// //Are you getting full text when echoing $textoesp echo "$textoesp"; ///END TEST-REMOVE if shown/// $charcount = strlen($textoesp); $mid = number_format($charcount/2, 0, '.', ''); $halflast = $mid-1; $cut = strpos($textoesp , " " , $halflast); $part1= substr($textoesp , 0 , $cut); $part2= substr($textoesp , $cut , $charcount); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> <style type="text/css"> <!-- body { margin-top: 0px; } --> </style> <link href="styles/cantera_styles.css" rel="stylesheet" type="text/css" /> </head> <body> <?php echo "<table><tr>"; // First column echo "<td>$part1</td>"; echo "<td style='width:30px'></td>"; // Second column echo "<td>$part2</td>"; echo "</tr></table>"; require_once('footer.php'); ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/237423-please-help-with-displaying-text-in-columns/#findComment-1220170 Share on other sites More sharing options...
lingo5 Posted May 25, 2011 Author Share Posted May 25, 2011 Thanks a lot Drummin it works!!!!!!! Quote Link to comment https://forums.phpfreaks.com/topic/237423-please-help-with-displaying-text-in-columns/#findComment-1220186 Share on other sites More sharing options...
xyph Posted May 25, 2011 Share Posted May 25, 2011 I suggest specifying your question in writing then You'll get less help if people need to go through 50 lines of code to figure out what you want. If you must use code to illustrate what you want, summarize it in as few lines as possible. Hope this function helps <?php // If $splitAfter is not defined, cut the string in half function split_at_word ( $string, $splitAfter = FALSE ) { if( is_numeric($splitAfter) && $splitAfter < 1 ) { preg_match( '/^.{'.$splitAfter.'}.*?\s/s', $string, $split ); return array( trim($split[0]), trim(substr($string, strlen($split[0]))) ); } elseif ( $splitAfter === FALSE ) { preg_match( '/\s/', $string, $split, PREG_OFFSET_CAPTURE, strlen($string) / 2 ); return array( trim(substr($string,0,$split[0][1])), trim(substr($string,$split[0][1])) ); } return FALSE; } $text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."; print_r( split_at_word( $text ) ); ?> Quote Link to comment https://forums.phpfreaks.com/topic/237423-please-help-with-displaying-text-in-columns/#findComment-1220187 Share on other sites More sharing options...
lingo5 Posted May 25, 2011 Author Share Posted May 25, 2011 Thanks to you too xyph Quote Link to comment https://forums.phpfreaks.com/topic/237423-please-help-with-displaying-text-in-columns/#findComment-1220189 Share on other sites More sharing options...
Drummin Posted May 25, 2011 Share Posted May 25, 2011 Curious xyph if your function takes into account an odd number of characters. Split in half give you a decimal value, 62.5 etc. Just wondering. Quote Link to comment https://forums.phpfreaks.com/topic/237423-please-help-with-displaying-text-in-columns/#findComment-1220190 Share on other sites More sharing options...
xyph Posted May 25, 2011 Share Posted May 25, 2011 It shouldn't make a difference. A float type-casted as an int should just lose it's decimal places. 62.5 becomes 62. Quote Link to comment https://forums.phpfreaks.com/topic/237423-please-help-with-displaying-text-in-columns/#findComment-1220292 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.