Jump to content

Recommended Posts

Hello Again for like the 5th time.  ;D

 

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?

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

 

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.

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.

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>";
    }
?>

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.

Worked perfect.

 

Thanks  ;D

 

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>");			
            }
?>

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.