简体   繁体   中英

php explode text from mysql database into table

I have a text field in my database where a new item, or subject is separated using "-"

I have successfully exploded the text and made it appear in a table where each item, or subject is displayed in a new row.

The problem I have is I want to be able to code this so that no matter how many items are in the text field the script will only echo that number of rows - as currently all blank cells are also echoed..

for example there text field is: "- hello - my - name - is - something" this would only fill the top 5 rows, leaving the others displaying blank.

thanks in advance!

echo "<table border=1 cellpadding=5 width=60% style='border-collapse:collapse; height:auto; font-size:small; float:left;'>
                <tr>
                    <th style='background-color:#FFFFCC';>This Weeks Objectives</th>
                </tr>";
            // split data by use of "-" into seperate rows          
            while ($row3 = mysql_fetch_assoc($objective)) {
                $str = $row3['objectives'];
                $splitStr = explode('-', $str);
                $int1 = intval($splitStr[0]);
                $int2 = intval($splitStr[1]);
                $int3 = intval($splitStr[2]);
                $int4 = intval($splitStr[3]);
                $int5 = intval($splitStr[4]);
                $int6 = intval($splitStr[5]);
                $int7 = intval($splitStr[6]);
echo "<tr>"; 
                    echo "<td style='background-color:#FFFFFF;text-align:left;'>" . $splitStr[1] . " " . $splitStr[2] . " </td>";
                echo "</tr>";
                echo "<tr>"; 
                    echo "<td style='background-color:#FFFFFF;text-align:left;'>" . $splitStr[2] . "</td>";
                echo "</tr>";
                echo "<tr>"; 
                    echo "<td style='background-color:#FFFFFF;text-align:left;'>" . $splitStr[3] . "</td>";
                echo "</tr>";
                echo "<tr>"; 
                    echo "<td style='background-color:#FFFFFF;text-align:left;'>" . $splitStr[4] . "</td>";
                echo "</tr>";
                echo "<tr>"; 
                    echo "<td style='background-color:#FFFFFF;text-align:left;'>" . $splitStr[5] . "</td>";
                echo "</tr>";
                echo "<tr>"; 
                    echo "<td style='background-color:#FFFFFF;text-align:left;'>" . $splitStr[6] . "</td>";
                echo "</tr>";
echo "</table>";

Use a loop. For instance:

$objectives = explode('-', $str);

foreach ($objectives as $objective) {
    echo "<tr><td style='background-color:#FFFFFF;text-align:left;'>" . $objective . "</td></tr>\n";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM