PHP - When To Use Echo To Display Functions Or Definitions??
Hello, all:
I have been having these odd display stuff when I'm either trying to display or call functions or definitions. Seems like they work best when ALWAYS using echo to call them, though they also work without it?? So, what gives? when or how should I use them??? Should Definitions or Functions always be used along with ECHO?? as you can see slightly confused... EXAMPLE: // Why does this line work??? note, see here I'm using echo several times in order to display right) <?php if(RIGHT_COLUMN_WIDTH != '0') { echo "<td valign='top' width='" . RIGHT_COLUMN_WIDTH . "px'>"; echo categories(); echo "</td>";} ?> // But this one doesnt display just right?? Note: see echo not used when categories() function is called, but isnt it still being called by first echo?? <?php if(RIGHT_COLUMN_WIDTH != '0') { echo "<td valign='top' width='" . RIGHT_COLUMN_WIDTH . "px'>" . categories() . "</td>";} ?> Similar TutorialsHello fellow php friends, Im having abit of trouble trying to echo out a statement at certain times of the day. for example how would you echo out 'Good Morning' between the times of 01am to 11.59am then echo out 'Good afternoon' between times of 12pm and 6pm and so on. Please help!!!!! Hello All New here and could use a bit of help. Basically I have 2 separate functions that echo their results out in an html page. My problem is the first function needs a value from the second function after it has run but needs to be echoed before the second for aesthetics. Here's a bit of code to explain. Code: [Select] <div class="bag"><br/><?php echo writeShoppingCart(); ?> </div> <div id="contents"> <?php echo showCart(); ?> </div> So basically to work correctly I need showCart to finishing before calling writeShoppingCart() I tried doing a redirect to the same page but it didn't like that. Is there another way around this that I'm not aware of? I suppose I could write a third function to get the value I need and call it before but I was hoping for a something a little cleaner. Cheers Hi, This text outputs itself when i use it for a form field in an echo " value=\"<? include(\"djmessage.txt\"); ?>\" Why is this? it normally works outside of an echo. Many thanks Hi there This code below displays the time in different timezones, from a base time of Los Angeles time, and it's working well for us (the time is pulled from the start date field of an event post type within Wordpress). However, if an event is at midday it puts 12:00 pm for the time which can cause confusion internationally as to whether it is midnight or midday. We'd like to display "12:00 noon" instead of "12:00 pm". Does anyone have any idea how this code could be adapted to do this? This code will be used in a Wordpress site so the way the time is displayed is set there. However, Wordpress doesn't have a way to change just this one things that we're looking to do which is to change '12:00 pm' to '12:00 noon'. Thanks! <?php $tas_start_datetime = tribe_get_start_date(); ?><strong>Los Angeles, USA: </strong><?php $date = new DateTime($tas_start_datetime, new DateTimeZone('America/Los_Angeles')); echo $date->format('l, F j\, g:i a') . "\n"; ?><br/><strong>Detroit, USA: </strong><?php $date->setTimezone(new DateTimeZone('America/Detroit')); echo $date->format('l, F j\, g:i a') . "\n"; ?><br/><strong>Accra, Ghana: </strong><?php $date->setTimezone(new DateTimeZone('Africa/Accra')); echo $date->format('l, F j\, g:i a') . "\n"; ?><br/><strong>London, UK: </strong><?php $date->setTimezone(new DateTimeZone('Europe/London')); echo $date->format('l, F j\, g:i a') . "\n"; ?><br/><strong>Lagos, Nigeria: </strong><?php $date->setTimezone(new DateTimeZone('Africa/Lagos')); echo $date->format('l, F j\, g:i a') . "\n"; ?><br/><strong>Auckland, New Zealand: </strong><?php $date->setTimezone(new DateTimeZone('Pacific/Auckland')); echo $date->format('l, F j\, g:i a') . "\n"; ?>
hello, I want to display the echo ''"; in the right place on my website in changepassword_form.html all messages are written in changepassword.php. how can i move these echo after entering the wrong password or username? changepassword.php <?php ........... //success //change password in db $querychange = mysql_query("UPDATE users SET password='$newpassword' WHERE username='$username'"); session_destroy(); echo 'Password Has Been Changed Successfully !'; //move these } else{ echo('new password doesnt match!'); //move these } } else { die('old password doesnt match!'); //move these } } else { echo ""; include 'changepassword_form.html'; exit(); } } else{ echo 'you must be logged '; } ?> move these echos somewhere to my html code how to do these?? I got a good one for you guys, I run 20 or so websites on my server all that have very similar coding structures. On all sites, I have a php script for a mysql table that logs visitors and I filter out bots(google, yahoo, etc, as many as I know of), but since many new ones are discovered weekly/monthly, I hate manually going through all sites and adding the new bot to the array of existing bots for detection. I know there are a few ways to do this, but I'd like to know the most efficient way of basically doing one function/task/opertation and it automatically updating all website's to know the new bots without manually modifying all 20 php files every time. The sites are all on the same server, but at different domains. a few ideas, which are prob not the best 1) have a universal text file that I could update that would be located in some file on one of my sites that just has a list of all bots and then each site would read from that text file to know what bots to filter. 2) have a huge php file to execute that has all connection info/passwords for every site and runs a php script to update the corresponding website's mysql table that would hold each bot as a row in the table. the script would just add a new row with the new bot info. so then ultimately when logging visitors, it would have to read the database each time 3) try to "include" a universal .php file that's located on one of my sites that has the array of all bots. But I've tried to include a file from another domain and I know that's a security error and won't include the file as a .php, but rather the file after it's been processed. There's gotta be a better solution. any ideas? Trying to figure out how to make it so name is a link to the profile when its echo anyone know how to do this? im at a huge stand still Code: [Select] <?php $sql = "SELECT name FROM users WHERE DATE_SUB(NOW(),INTERVAL 5 MINUTE) <= lastactive ORDER BY id ASC"; $query = mysql_query($sql) or die(mysql_error()); $count = mysql_num_rows($query); $i = 1; while($row = mysql_fetch_object($query)) { $online_name = htmlspecialchars($row->name); echo '<a href="Inbox.php">"'[$goauld]'</a>"'; ?> Do interfaces in PHP include default methods, which are available in Java 7 and 8. Default methods allow you to add default functionality in the interface itself. So when a class implements it, it can use the default definition from the interface. This is in contrast to an abstract method which has no method body.
The below might be a noob question but, I concatenate two spaces in the below string $new_hebrew_date_string = $hebrewMonthName . ' ' . $hebrewDay . "%"; yet when I execute echo "<br />new_hebrew_date_string is $new_hebrew_date_string<br />"; printf("hebrew_date_string is: %s <br />", $new_hebrew_date_string); Only one space is output in between $hebrewMonthName and $hebrewDay regardless of how many spaces I pad the string with. Strangely enough, the string $new_hebrew_date_string is correctly padded w/spaces when I pass it as a parameter to a mySQL stored procedure. Any ideas anyone? Thank you. hi there, the code below is suppose to display something like the attachement "code2" but instead it displays something like attachement "code1" please assist in find what is wrong with my echo lines.
<?php $counter = 2; $sqlq="select * from state WHERE status = 0 "; $categorysqlq = mysql_query($sqlq); $varq = mysql_num_rows($categorysqlq); while($catfetchq = mysql_fetch_array($categorysqlq)) { $cnty = $catfetchq[0]; $sqllq="select * from vehicle WHERE country = '$cnty' "; $categorysqllq = mysql_query($sqllq); $numsql = "select * from branchaddr WHERE state = '$cnty' "; $numquery = mysql_query($numsql); $varqa = mysql_num_rows($numquery); $cntyfetchq= mysql_fetch_array($numquery); if($varq != 0){ if($counter == 2){ echo "<tr><td><a href=\"transport2.php?id=".$cntyfetchq['state'].">".$catfetchq[1]."(<span style=\"color:red\">".$varqa."</span>)</a></td>"; $counter--; } else{ echo "<td><a href=\"transport2.php?id=".$cntyfetchq['state'].">".$catfetchq[1]."(<span style=\"color:red\">".$varqa."</span>)</a></td></tr>"; $counter = 2; } } } ?>Attached Files code1.png 12.65KB 0 downloads code2.png 25.34KB 0 downloads OK, have no idea what's going on... I've done this a million times... why wont this output!?? I must have a major brain meltdown and dont know it yet!!! Code: [Select] <?php // this echoes just fine: echo $_POST['testfield']; // but this wont echo: echo if (isset($_POST['testfield'])) { $_POST['testfield'] = $test; } echo $test; /// or even this DOESNT echo either!: $_POST['testfield'] = $test; echo $test; ?> How to get this echo line to display as one line? No matter what I have done it displays as two lines. I even tried <nobr></nobr> Teachers Name: John Jones $userid = mysql_real_escape_string($_GET['user_id']); $sql = "select * from users where `id`='$userid' "; $rs = mysql_query($sql); while($row = mysql_fetch_array($rs)) { echo "<h3>Teachers Name: </h3>" . $row["first_name"] . " " . $row["last_name"] ; } Thanks for your help. I have a script I am putting together that simulate a cricket game. The only issue is, that there are a huge number of functions because there doesn't seem to be any other way to do this properly. As well as this, there a while() loop and all this seems to be leading to the page reaching a max 30 second timeout when generating the result. My code is attached below, it is quite messy at the moment because i've just be working on it, but I was wondering if anyone has any solutions of how I can speed this up or change to prevent a timeout: <?php // Error reporting error_reporting(E_ALL); // Connect DB mysql_connect("wickettowicket.adminfuel.com", "rockinaway", "preetha6488") or die(mysql_error()); // Select DB mysql_select_db("wickettowicket") or die(mysql_error()); // MySQL queries to find batsmen and bowlers $array_batsmen = mysql_query('SELECT id, name, ability, strength FROM wtw_players WHERE team_id = 1 ORDER BY id ASC'); $array_bowlers = mysql_query('SELECT id, name, ability, strength FROM wtw_players WHERE team_id = 2'); // Start table for data $data = '<table width="600px">'; // Create blank scorecard while ($array_bat = mysql_fetch_array($array_batsmen)) { $data .= '<tr><td>'.$array_bat['name'].'</td><td></td><td></td><td>0</td></tr>'; } // Set up arrays for players $current_batsman = $current_bowler = array(); // Reset query mysql_data_seek($array_batsmen,0); $in_one = $in_two = $it = ''; function currentBatsman($id, $name, $ability, $strength, $out, $in, $runs) { global $current_batsman; $current_batsman = array ( 'id' => $id, 'name' => $name, 'ability' => $ability, 'strength' => $strength, 'out' => $out, 'in' => $in, 'runs' => $runs ); echo 'set current'; } // Set up arrays of batsmen while ($array = mysql_fetch_array($array_batsmen)) { if ($it < 3 && $in_one == '') { currentBatsman($array['id'], $array['name'], $array['ability'], $array['strength'], 0, 1, 0); $batsmen[$array['id']] = array ( 'id' => $array['id'], 'name' => $array['name'], 'ability' => $array['ability'], 'strength' => $array['strength'], 'out' => 0, 'in' => 1, 'runs' => 0 ); $in_one = $array['id']; $current = $array['id']; $it++; } else if ($it < 3 && $in_two == '') { $batsmen[$array['id']] = array ( 'id' => $array['id'], 'name' => $array['name'], 'ability' => $array['ability'], 'strength' => $array['strength'], 'out' => 0, 'in' => 1, 'runs' => 0 ); $in_two = $array['id']; $it++; } else { $batsmen[$array['id']] = array ( 'id' => $array['id'], 'name' => $array['name'], 'ability' => $array['ability'], 'strength' => $array['strength'], 'out' => 0, 'in' => 0, 'runs' => 0 ); } } // Bowler Array while ($array = mysql_fetch_array($array_bowlers)) { $bowlers[] = array ( 'name' => $array['name'], 'ability' => $array['ability'], 'strength' => $array['strength'] ); } // Reset both queries mysql_data_seek($array_bowlers,0); mysql_data_seek($array_batsmen,0); function changeBatsman($just_out) { global $array_batsmen, $batsmen; //Update array $batsmen[$just_out] = array ( 'in' => 1, 'out' => 1 ); while ($array = mysql_fetch_array($array_batsmen)) { if ($just_out != $array['id'] && $batsmen[$array['id']]['out'] != 0) currentBatsman($array['id'], $array['name'], $array['ability'], $array['strength'], 0, 1, 0); } // Reset query mysql_data_seek($array_batsmen,0); echo 'change batsman'; } function swapBatsman($other_batsman) { global $array_batsmen, $batsman; while ($array = mysql_fetch_array($array_batsmen)) { if ($other_batsman != $array['id'] && $batsman[$array['id']]['out'] != 0 && $batsman[$array['id']]['in'] == 1) currentBatsman($array['id'], $array['name'], $array['ability'], $array['strength'], 0, 1, 0); } // Reset query mysql_data_seek($array_batsmen,0); echo 'swap batsman'; } $runs = $outs = $balls = $overs = 0; $played = array(); function selectBowler() { global $bowlers, $current_bowler; // Select random bowler $choose_bowler = array_rand($bowlers, 1); $current_bowler = array ( 'name' => $bowlers[$choose_bowler]['name'], 'ability' => $bowlers[$choose_bowler]['ability'], 'strength' => $bowlers[$choose_bowler]['strength'] ); } /* function selectBatsman(); { global $array_batsmen; while ($array_batsmen[]['out'] != 1) { }*/ function bowl() { global $batsmen, $bowlers, $current_bowler, $current_batsman, $data, $balls, $outs, $runs; if ($current_batsman['out'] == 0) { echo 'bowling'; // Set the initial number $number = rand(0, 190); // Ability of batsman if ($current_batsman['ability'] > 90) $number += 30; else if ($current_batsman['ability'] > 70) $number += 15; else if ($current_batsman['ability'] > 50) $number += 2; else $number = $number; // Strength of batsman if ($current_batsman['strength'] > 90) $number += 15; else if ($current_batsman['strength'] > 70) $number += 10; else if ($current_batsman['strength'] > 50) $number += 5; else $number = $number; // Depending on overs if ($balls > 270) $number += 30; else if ($balls > 120) $number -= 10; // Ability if ($current_bowler['ability'] > 90) $number -= 30; else if ($current_bowler['ability'] > 70) $number -= 15; else if ($current_bowler['ability'] > 50) $number -= 2; else $number = $number; // If batsman has made a huge total of runs, we need to knock some numbers off - more likely to get out if ($current_batsman['runs'] > 200) $number -= 70; else if ($current_batsman['runs'] > 100) $number -= 30; // Finally sort out runs if ($number > 190) $run = 6; else if ($number > 170) $run = 4; else if ($number > 160) $run = 3; else if ($number > 100) $run = 2; else if ($number > 50) $run = 1; else if ($number > 10) $run = 0; else if ($balls > 120 && $number > 0) $run = 0; else $run = -1; // Increase number of balls $balls += 1; // Are they out? if ($run == -1) { $current_batsman['out'] = 1; $played[] = $current_batsman['id']; $find = '<tr><td>'.$current_batsman['name'].'</td><td></td><td></td><td>0</td></tr>'; $replace = '<tr><td>'.$current_batsman['name'].'</td><td></td><td>'.$current_bowler['name'].'</td><td>'.$current_batsman['runs'].'</td></tr>'; $data = str_replace($find, $replace, $data); changeBatsman($current_batsman['id']); echo 'out'; } else { $current_batsman['runs'] += $run; $runs += $run; if ($run == 1 || $run == 3) { swapBatsman($current_batsman['id']); echo 'time to swap'; } echo $run; } // Count outs if ($current_batsman['out'] == 1) $outs += 1; } } function game() { global $main, $batsmen, $bowlers, $data, $batted, $balls, $outs, $current_batsman; // Check if possible while ($balls <= 295 && $outs < 10) { selectBowler(); // Actually bowl now bowl(); } } game(); echo $data; Hi All, I'm trying to echo the response from an SLA query, the query works and returns the data when I test it on an SQL application.. but when I run it on my webpage it won't echo the result. Please help? <?php $mysqli = mysqli_connect("removed", "removed", "removed", "removed"); $sql = "SELECT posts.message FROM posts INNER JOIN threads ON posts.pid=threads.firstpost WHERE threads.firstpost='1'"; $result = mysqli_query($mysqli, $sql); echo {$result['message']}; ?> I teaching myself php, but I am coming from java and other compiled languages, so the process has been a little bumpy. I am trying to do something like this: Code: [Select] class my_class { function one () { $two = two (); $three = three (); $five = $two + $three; return $five; } function two () { $two = 2; return $two; } function three () { $three = 3; return $three; } } Unfortunately, I keep getting an error message saying that my call to two () is an undefined function. I am gathering from this that the scope of one () is not aware of the existence of two (). Is there a way to get around this so I can call two () and three () from one ()? I have a log system that allows 10 logs on each side(Left and right). I am trying to make it so that the left side has the 10 most recent logs, then the right as the next 10. Any ideas? So I need to echo a row from my database with php, but where i need to echo is already inside an echo. This is my part of my code: $con = mysql_connect("$host","$username","$password"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("main", $con); $result = mysql_query("SELECT * FROM Vendor"); while($row = mysql_fetch_array($result)) { //I need to echo right here .................. but I get a blank page when I try this. Please Help. echo '<option value=$row['vendor_id']>'; echo $row['vendor_id']; echo '</option>'; } mysql_close($con); Result: A Blank page. Thanks in advance! Hi
I try to echo out random lines of a html file and want after submit password to whole content of the same html file. I have two Problems.
1st Problem When I echo out the random lines of the html file I don't get just the text but the code of the html file as well. I don't want that. I just want the text. How to do that?
for($x = 1;$x<=40;$x++) { $lines = file("$filename.html"); echo $lines[rand(0, count($lines)-1)]."<br>"; }I tried instead of "file("$filename.html");" "readfile("$filename.html");" But then I get the random lines plus the whole content. Is there anything else I can use instead of file so that I get the random lines of text without the html code?P.S file_get_contents doesn't work either have tried that one. 2nd Problem: As you could see in my first problem I have a file called $filename.html. After I submit the value of a password I want the whole content. But it is like the program did forget what $filename.html is. How can I make the program remember what $filename.html is? Or with other words how to get the whole content of the html file? My code: if($_POST['submitPasswordIT']){ if ($_POST['passIT']== $password ){ $my_file = file_get_contents("$filename.html"); echo $my_file; } else{ echo "You entered wrong password"; } }If the password isn't correct I get: You entered wrong password. If the password is correct I get nothing. I probably need to create a path to the file "$filename.html", but I don't know exactly how to do that. Hi. I am trying to write a function but they are new to me and confusing me. On multiple pages I am requesting users details. I thought instead of writing out the query every time I want to do this I would include a functions page with the query in it. The only variable I carry from the user is the user name as a session when they log in. The user name is the email address of the person signed in. so here is my first function. As im sure you can see it doesn't work but I don't understand why or how to call it properly. Can someone please explain.... function GetUser($user = "") { $qFindUser = "SELECT * FROM members WHERE email = '$user'"; $rFindUser = mysql_query($qFindUser) or die (mysql_error()); $UserInfo = mysql_fetch_array($rFindUser); } calling the function to return all details of a member as an array. <?php $Name = $_SESSION['MM_Username']; $UserInfo = GetUser($Name); echo $UserInfo['forename']; ?> Hi, I could use a little guidance here. 1) is a function with values to be sent to 2). 1) <!-- use ajax - OnCalc function to send data to your database. --> function OnCalc(value1,op,value2,total) { 2) php file using above function values <?php header( "refresh:5;url='http://localhost/hom...lcprint.php'"); echo 'You\'ll be redirected in about 5 secs. If not, click <a href="http://localhost/hom...hp">here</a>.'; echo '<script type="text/javascript">' , 'OnCalc();' , '</script>'; No error but code not working? |