June 14, 2012

echo font color output

Question by xan

I wrote a php code that display error in red color. But somehow, this doesn’t seem to work out. Here’s my code:

<?php
...    
if(!$name || !$email || !$contact || !$itemid){
                        //if not display an error message
                        echo "<span style="color: red;" /><center>Fields marked with <strong>&#40; &#42; &#421</strong> are mandatory!</center></span>";
                        }else{...


?>

Answer by swapnesh

Do this something like that —

echo '<span style="color: red;" /><center>Fields marked with <strong>&#40; &#42; &#421</strong> are mandatory!</center></span>';

Your "" quotes are conflicting

Answer by Starx

The problem is un-escaped quotes on your PHP expression.

echo "<span style="color: red;" />...
                //^ Right here        

Because, Your PHP echo statement also started with the same quote i.e ".

Here are the different ways you can solve this:

  1. Use mixed quotes

    echo "<span style='color: red;' />...
          // Single quote in the HTML part
    
  2. Escape the quotes

    echo "<span style="color: red;" />...
         // Escape the quotes so that it gets treated as string rather than a modifier
    
April 5, 2012

echo plus sign php

Question by jose

I’m echoing a php string variable into html

<span class="title_resource">
  <?php echo  $titulo; ?>
</span>

But when the plus sign (+) is present, it turns into a space.

I’ve already tried using urlencode but then I get something like “Constru%25C3%25A7%25C3%”

Should I use something like string replace?

Thanks in advance

Update:

$titulo is setted using $_GET

if (isset($_GET['T']))// title
    $titulo = $_GET['T'];

(…)

More clear, perhaps

I want to send exactly this text “Estudo de ax^2 + bx + c”. The page receives by $_GET. When I print this value I get “Estudo de ax^2 bx c”

Answer by Starx

Its the way values as encoded to be sent over using GET, spaces are converted to + and + are converted to %2B.

If you really want to send the plus symbol over the form, then replace the spaces with plus

$text= str_replace(" ", "+", $text);

Next make sure your form uses correct enctype either application/x-www-form-urlencoded or multipart/form-data

April 3, 2012

Can AJAX or PHP "echo" too much?

Question by Marc Brown

This a question that is more on the lines of server efficiency and availability.

Say I have a php page that calls a AJAX script and 1 million users are connecting within the same second.

Would there be a performance boost if I limit the AJAX script so that it only echos once instead of every time it receives data?

I planned on printing all the data to a variable and then echoing that variable once the script is finished.

I’m not sure if echoing is simply storing the data in the server until the script finishes, similar to what I want to do above or if it is actually connecting with the client each time?

If a connection is made for each echo then that would be better than filling a variable with data, possibly causing the RAM to fill up fast?

This AJAX script is pulling data from a database (calls PHP page). I have a lot of “echo” statements that are simply printing table, div, tr, etc tags and then finally the data from the database. Then, once again printing table, div, tr, etc tags. Your saying that it’s better to simply fill a variable with this data and print/echo ONCE?

Thanks,

Answer by Starx

Generally, servers processes scripts and sends the output in the form of HTML, so there is no longer link between a page and server. When you are making a AJAX request, you will open the connection again and send the request.

The main bottleneck in the performance occurs from the amount of request you send to the server. You should limit the request as much as possible.

If a connection is made for each echo then that would be better than filling a variable with data, possibly causing the RAM to fill up fast.

You are wrong about this, You should minimize the request as much as possible, especially, when you are processing a large number of people.


Next, Its nearly pointless to update the content onevery pass. Look how facebook updates its content. The timers and contents are updated every minute. Except the notification part.

March 5, 2012

How to echo a php array for dynamic javascript

Question by ToddN

I am using Google Analytics API to pull Total Visits and put into Googles Javascript to display in a bar graph. I am having issues trying to put an array to the variables. When I do a print_r it spits out everything just fine but I need it to spit out everything just as it is. This is what I got so far:

//I connect to my own SQL database for $num

//Connect to Google Analytics
$mystart = '2012-02-28';

$i=0;
while ($i < $num) {

        $ga->requestReportData(ga_profile_id,array('source'),array('visits', 'visitBounceRate'),'-visits', $filter, $mystart, $mystart);

//adds +1 day to date 
$mystart = date('Y-m-d', strtotime("+1 day", strtotime($mystart)));

$totalvisits = $ga->getVisits();

//These are my arrays  
$adddate[] = "data.addColumn('number', '" . $mystart . "');";
$addvisits[] = "$totalvisits,";
        }

This is what I am trying to achieve through the use of echos:

<script type="text/javascript">
      function drawChart() {
        var data = new google.visualization.DataTable();
        data.addColumn('string', 'Date');
        // This is where I want to put something like <? echo $adddate ?> but of course doesn't work
        data.addColumn('number', '2012-02-28');
        data.addColumn('number', '2012-02-29');
        data.addColumn('number', '2012-03-01');
        data.addColumn('number', '2012-03-02');
        data.addColumn('number', '2012-03-03');
        data.addRows([
        // This is where I want to put something like <? echo $addvisits ?> but of course doesn't work
          ['Feb. 28 to March 3', 100, 105, 91, 80, 150]
        ]);
</script>

Answer by Ynhockey

If you are asking how to output the array in the way you want, then use something like:

echo implode("rn", $adddate);

See implodeDocs.

Answer by Starx

Of course, it will work, you cannot echo an array. The easiest way is to implode them

Use these codes, where you are echoing them right now

echo implode("",$adddate);
echo implode("",$addvisits);
// User rn to implode if you need to add linebreaks
...

Please fill the form - I will response as fast as I can!