cuboidgraphix Posted April 9, 2008 Share Posted April 9, 2008 Hello Again for like the 5th time. I know I'm taking this for a habit, but I have no where else to turn to. I'm not that experienced ... Anyways... can someone help me add a String Replace function into the array? The code is as follows.. <?php $fecha=date('Y-m-d'); mysql_connect("localhost","user","pass") or die(mysql_error()); // connect to database mysql_select_db("database") or die(mysql_error()); // select the database $query2 = "SELECT Index, Status, Activity, Date, Time, Source, Severity, Fault Name, Sent FROM alarms WHERE severity = 'CRITICAL' AND date = '$fecha' OR severity = 'MAJOR' AND date = '$fecha' ORDER BY time DESC"; $data2 = mysql_query("$query2;") or die(mysql_error()); // Color2 Coding function color2($field, $value){ switch ($field){ case "Activity": if($value == "Active"){ $color = "red"; } else { if($value == "Clear"){ $color = "green"; } } break; case "Severity": if($value == "CRITICAL"){ $color = "red"; } else { if($value == "MAJOR "){ $color = "orange"; } elseif($value == "MINOR"){ $color = "yellow"; } else { $color = "green"; } } break; case "Sent": if($value == "0"){ $color = "red"; } else { if($value == "1"){ $color = "green"; } } break; default: $color = ""; } return $color; } // Table Output print "<table width=\"800\">"; print "<tr>"; for($i = 0;$i < mysql_num_fields($data2);$i++) { $fieldname = mysql_field_name($data2, $i); print "<th>" .$fieldname . "</th>"; } print "</tr>"; for ($i = 0; $i < mysql_num_rows($data2); $i++) { print "<tr>"; $row = mysql_fetch_row($data2); for($j = 0;$j < mysql_num_fields($data2);$j++) { $fieldname = mysql_field_name($data2, $j); echo("<td class=\"".color2($fieldname, $row[$j])."\">" . $row[$j] . "</td>"); } print "</tr>"; } print "</table>"; print "<div align=\"right\"><a href=\"bsm-alarm-alarms.php\">View All</a></div>"; ?> OK now... the problem is in the table output. The 'Source' Field outputs a long string, unbreakable .... something like this : O%:CBS1:CIS1:PrimaryDISCOCSubsystem1:Root1:BCNPort22O%:CBS1:CIS1:PrimaryDISCOCSubsystem1:Root1:BCNPort22. I need to use the string replace to replace the colons ( : ). I was thinking something like: $src = $row['source']; $var = array(":"); $var_rep = str_replace($var, " ", "$src"); but how to fit this or this concept int my $row = mysql_fetch_row($data2); Can anybody help? Quote Link to comment https://forums.phpfreaks.com/topic/100399-solved-please-help-problems-with-a-string-replace/ Share on other sites More sharing options...
amites Posted April 9, 2008 Share Posted April 9, 2008 Not sure what your asking, if you want to use a str replace on your output, you could process it after you get a result, $data2 = mysql_query($query2) or die(mysql_error()); while ($row = mysql_fetch_assoc($data2)) { $no_colons = str_replace(':', " ", $row['field']); } if you want to change the DB, add an update line to that while loop, if you're trying to do something else please think through the details of what you want to do, good chance you'll come up with an answer while writing your question Quote Link to comment https://forums.phpfreaks.com/topic/100399-solved-please-help-problems-with-a-string-replace/#findComment-513444 Share on other sites More sharing options...
doni49 Posted April 10, 2008 Share Posted April 10, 2008 think through the details of what you want to do, good chance you'll come up with an answer while writing your question Sounds like good advice to me. I don't know how many times, the answer has just dawned on me while I was trying to figure out how to explain my question. Quote Link to comment https://forums.phpfreaks.com/topic/100399-solved-please-help-problems-with-a-string-replace/#findComment-513487 Share on other sites More sharing options...
cuboidgraphix Posted April 10, 2008 Author Share Posted April 10, 2008 Hi Guys... Sorry for not being too clear. I had used that WHILE before, and it did work with that, just that in this case I'm using the FOR. To me the script provided to me (the one I posted) is a little more complicated. I'm trying to use the string replace into this part of the script. <?php for ($i = 0; $i < mysql_num_rows($data2); $i++) { print "<tr>"; $row = mysql_fetch_row($data2); for($j = 0;$j < mysql_num_fields($data2);$j++) { // I want to put the string replace somewhere in here for the rows.. $fieldname = mysql_field_name($data2, $j); echo("<td class=\"".color2($fieldname, $row[$j])."\">" . $row[$j] . "</td>"); } print "</tr>"; } ?> if you take a look at the Color Coding Script on my first post, you'll see how a specific field/column was targeted. I was wondering if the same can be done only including a string replace function for that column. I hope I've explained myself a lil bit better this time. Quote Link to comment https://forums.phpfreaks.com/topic/100399-solved-please-help-problems-with-a-string-replace/#findComment-513868 Share on other sites More sharing options...
cuboidgraphix Posted April 10, 2008 Author Share Posted April 10, 2008 Anyone? Please.. Quote Link to comment https://forums.phpfreaks.com/topic/100399-solved-please-help-problems-with-a-string-replace/#findComment-514031 Share on other sites More sharing options...
cuboidgraphix Posted April 10, 2008 Author Share Posted April 10, 2008 ??? Quote Link to comment https://forums.phpfreaks.com/topic/100399-solved-please-help-problems-with-a-string-replace/#findComment-514227 Share on other sites More sharing options...
soycharliente Posted April 10, 2008 Share Posted April 10, 2008 Did you try to add in the code that was given to you? I don't see you saying that you attempted to use it at all. Just insert the code in where you get the data at. <?php for ($i = 0; $i < mysql_num_rows($data2); $i++) { print "<tr>"; $row = mysql_fetch_row($data2); for($j = 0;$j < mysql_num_fields($data2);$j++) { $fieldname = mysql_field_name($data2, $j); $no_colons = str_replace(':', " ", $fieldname); echo("<td class=\"".color2($no_colons, $row[$j])."\">" . $row[$j] . "</td>"); } print "</tr>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/100399-solved-please-help-problems-with-a-string-replace/#findComment-514230 Share on other sites More sharing options...
cuboidgraphix Posted April 10, 2008 Author Share Posted April 10, 2008 Hi ... It sorta worked. for($i = 0;$i < mysql_num_fields($data2);$i++) { $fieldname = mysql_field_name($data2, $i); print "<th>" .$fieldname . "</th>"; } print "</tr>"; for ($i = 0; $i < mysql_num_rows($data2); $i++) { print "<tr>"; $row = mysql_fetch_row($data2); for($j = 0;$j < mysql_num_fields($data2);$j++) { $fieldname = mysql_field_name($data2, $j); $no_colons = str_replace(':', " ", $row); echo("<td class=\"".color2($fieldname, $no_colons[$j])."\">" . $no_colons[$j] . "</td>"); } The thing is that it targets all the fields. I need it to only target the 'Source' Field. Any further input that can help me out? I'd be very grateful. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/100399-solved-please-help-problems-with-a-string-replace/#findComment-514287 Share on other sites More sharing options...
cuboidgraphix Posted April 11, 2008 Author Share Posted April 11, 2008 ??? Quote Link to comment https://forums.phpfreaks.com/topic/100399-solved-please-help-problems-with-a-string-replace/#findComment-514817 Share on other sites More sharing options...
soycharliente Posted April 11, 2008 Share Posted April 11, 2008 <?php if ($fieldname === "source") { // do what you want to do } ?> Quote Link to comment https://forums.phpfreaks.com/topic/100399-solved-please-help-problems-with-a-string-replace/#findComment-514818 Share on other sites More sharing options...
cuboidgraphix Posted April 11, 2008 Author Share Posted April 11, 2008 Worked perfect. Thanks Here's the final code. <?php for($i = 0;$i < mysql_num_fields($data2);$i++) { $fieldname = mysql_field_name($data2, $i); print "<th>" .$fieldname . "</th>"; } print "</tr>"; for ($i = 0; $i < mysql_num_rows($data2); $i++) { print "<tr>"; $row = mysql_fetch_row($data2); for($j = 0;$j < mysql_num_fields($data2);$j++) { $fieldname = mysql_field_name($data2, $j); if ($fieldname === "Source") { $no_colons = str_replace(':', " ", $row); } else { $no_colons = $row; } echo("<td class=\"".color2($fieldname, $no_colons[$j])."\">" . $no_colons[$j] . "</td>"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/100399-solved-please-help-problems-with-a-string-replace/#findComment-514843 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.