Jump to content

Recommended Posts

I want be able to prevent my code from displaying html content around a specific exploded variable; if the exploded variable is null. Right now I have multiple if and else conditions; but I want to simplify that; with maybe a for loop or while loop. I am not quit sure what would be the best way with going about doing this.

 

Here is the code I have so far.

 

<?php


$myvar="Driver Presets, 6 Disc CD Player, DVD Player, Heated Seats, On-Star Navigation System, Play Station 3 Gaming Center, Dual Heat And Air, Rear Camera Monitor, Leather Seats";


$seperate = explode(", ", $myvar);






// Validation To Eliminate Empty List
// This is what I want to simplify

if ($seperate[0] != NULL)
{
$lio1="<li>";
$lic1="</li>";
}
else {
$lio1="";
$lic1="";
}
if ($seperate[1] != NULL)
{
$lio2="<li>";
$lic2="</li>";
}
else {
$lio2="";
$lic2="";
}
if ($seperate[2] != NULL)
{
$lio3="<li>";
$lic3="</li>";
}
else {
$lio="";
$lic="";
}
if ($seperate[3] != NULL)
{
$lio4="<li>";
$lic4="</li>";
}
else {
$lio4="";
$lic4="";
}
if ($seperate[4] != NULL)
{
$lio5="<li>";
$lic5="</li>";
}
else {
$lio5="";
$lic5="";
}
if ($seperate[5] != NULL)
{
$lio6="<li>";
$lic6="</li>";
}
else {
$lio6="";
$lic6="";
}
if ($seperate[6] != NULL)
{
$lio7="<li>";
$lic7="</li>";
}
else {
$lio7="";
$lic7="";
}
if ($seperate[7] != NULL)
{
$lio8="<li>";
$lic8="</li>";
}
else {
$lio8="";
$lic8="";
}
if ($seperate[8] != NULL)
{
$lio9="<li>";
$lic9="</li>";
}
else {
$lio9="";
$lic9="";
}







echo "<table width=700px><ul><td valign=top>";

echo "$lio1$seperate[0]$lic1$lio2$seperate[1]$lic2$lio3$seperate[2]$lic3";

echo "</td><td valign=top>";

echo "$lio4$seperate[3]$lic4$lio5$seperate[4]$lic5$lio6$seperate[5]$lic6";

echo "</td><td valign=top>";

echo "$lio7$seperate[6]$lic7$lio8$seperate[7]$lic8$lio9$seperate[8]$lic9";

echo "</td></ul></table>";

?>

 

I want to be able to eliminate having to name a different variable for each <li> and </li>. I want to be able to only have to use one variable for the <li> and only one variable for the </li>.

Not 100% sure what you're trying to achieve. I think you wanted something like:

 

<?php
$str = 'Driver Presets, 6 Disc CD Player, DVD Player, Heated Seats, On-Star Navigation System, Play Station 3 Gaming Center, Dual Heat And Air, Rear Camera Monitor, Leather Seats';
$bits = explode(',',$str);
echo '<ul>';
foreach($bits as $v){
	$v = trim($v);
if(!empty($v)){
	echo '<li>'.$v.'</li>';
}
}
echo '</ul>';
?>

I'm feeling nice so I'll explain a lot.  First you ain't validating anythign cause you have a static array you are checking for blanks that makes no sense you want to compare to the post data or get data or mysql data.

 

try this

<?php
$upgrades = array("Driver Presets", "6 Disc Player", "DVD Player", "Heated Seats", "On-Star Navigation System");
echo "<ul>";
foreach($upgrades as $value){
echo "<li>".$value."</li>";
}
echo "</ul>";
?>

That doesn't validate anything but is 100 times cleaner

you still do not accomplish what i want to do.

 

I want a table with 3 individual <td>; in each <td> I want to have 3 list items. with the code you have; there is no table. I was validating with and if/else condition; but I wanted to simplify it. I want to be able to display only list items where there is a need for the list item. the list item would be pulled from the exploded variable.

 

ps: thanks for being so nice guy - this forum is for helping each other with our code; not trying to be caviler about our coding skills

so you trying to get a table 3 wide with all these things

try what i have except edit the foreach a bit

<?php
//Arrray
$i = 0;
echo "<table>";
foreach($array as $value){
if($i%3 == 0 && $i >0){echo "</tr>";}
if($i%3 == 0){echo "<tr>";}
echo "<td>".$value."</td>";
$i++;
}
echo "</tr></table.";
?>

 

But I don't understand what your validating.  Validating is done off a user's input or database data not off an array YOU MADE.  If you make it why should it need validation because you can make it perfect to start with you ain't a dumb end user who can't enter the data right.

right now I am using a static string for "$myvar" variable, but when I get this code the way I want to; the

"myvar" will be a database variable.

 

here is an example of what I am trying to do:

 

  • Item 1
  • Item 2
  • Item 3

  • Item 4
  • Item 5
  • Item 6

  • Item 7
  • Item 8
  • Item 9

 

and if there was a missing item; it would display like this:

 

  • Item 3
  • Item 4
  • Item 5

  • Item 6
  • Item 7
  • Item 8

  • Item 9

 

Do you see what I am trying to accomplish?

I want a table with 3 individual <td>; in each <td> I want to have 3 list items. with the code you have; there is no table. I was validating with and if/else condition; but I wanted to simplify it. I want to be able to display only list items where there is a need for the list item. the list item would be pulled from the exploded variable.

What you doing ain't making too much sense, you say table and keep going back to list items why??

 

Just use what I gave you to generate the table, and then add a logical part to it so if(str_len($value) >0){echo $value;} and still echo the tds

Well ive no idea if this is actually what you wanted - im getting more and more lost by your description, but this would produce a table with 3 columns, a list in each:

 

<?php
$str = ', 6 Disc CD Player, DVD Player, Heated Seats, On-Star Navigation System, Play Station 3 Gaming Center, Dual Heat And Air, Rear Camera Monitor, Leather Seats';
$bits = explode(',',$str);
$array = array();
foreach($bits as $k =>$v){
	$v = trim($v);
if(!empty($v)){
	$array[] = $v;//the new array only contains those not empty strings
}
}
$num = count($array);
$cols = 3;
$per_col = ceil($num/$cols);
$i=0;
echo '<table border="1"><tr>';
foreach($array as $v){
if($i==0){
	echo '<td valign="top"><ul>';
}
echo '<li>'.$v.'</li>';
if(++$i == $per_col){
	echo '</ul></td>';
	$i = 0;
}
}
echo '</tr></table>';
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.