I am creating a website for a church and in the events section of the code i have this
$page_contents = mysql_query("SELECT * FROM events WHERE id = '$page_content'");
$contents = mysql_fetch_array($page_contents)or die (mysql_error());
echo "<Script>alert(".$contents['date'].")</script>";
I have 3 entries in the database and the dates are 0000-00-00, 2013-06-14 & 2013-06-19
The $page_content is the $_GET page id this comes through as events/$1/ and changes through .htacess to events.php?page=$1
when date 2013-06-14 is called the above echo script gives out 1993 and when date 2013-06-19 is called i get 1988
I have none of these years in my database or anything representing these in my code ? this has completely baffled me ????
Your output is not a string since you're missing the quotes in the Javascript you're outputting. It actually looks like this:
alert(2013-06-14)
Yeah, those are numbers, which are being subtracted...
Related
I'm working with WHMCS 5.3.x which has Smarty 2.x IIRC.
So for instance {$service.regdate} renders:
2015-06-01
What I need to do is add one month to that date:
2015-07-01
...which I can then format with:
|date_format:"%m/%d/%Y"
...to display it how I want it.
I've tried quite a few things based on Google results like:
{"+1 month $service.regdate"|date_format:"%m/%d/%Y"}
{strtotime("+1 month",$service.regdate)|date_format:"%m/%d/%Y"}
...and a few other various iterations but the output generally renders only my initial date or nothing at all.
I'd also need to generate the number of days left from $smarty.now to $service.regdate + 1 month so I can show my customers how many days are left.
This I haven't found nearly as much information on.
I know you can do most if not all of this in PHP and then assign it to Smarty variables, but, I haven't found an example that works and, if you've ever worked with WHMCS support before, they don't really give you much to go on other than to "ask the forums", "talk to modulesgarden" or "open a feature request"
Thank you =)
Sylvain is 100% correct and using MySQL is the proper resolution:
{php}
$userid = $this->_tpl_vars['clientsdetails']['id'];
$result = mysql_query("
SELECT *,
DATE_ADD(tblhosting.regdate,INTERVAL 1 MONTH) as trialexpires,
DATEDIFF(DATE_ADD(tblhosting.regdate,INTERVAL 1 MONTH),CURDATE()) as trialdaysleft
FROM tblhosting,tblproducts
WHERE userid = $userid
AND tblhosting.packageid= tblproducts.id
AND tblhosting.domainstatus='Active'"
);
$services = array();
while ($data = mysql_fetch_array($result)) {
array_push($services, $data);
}
$this->_tpl_vars['services'] = $services;
{/php}
Thank you =)
I assume, I need Ajax and jQuery to show new data based on date after clicking a button.
What I want to do is something like this:
<< January 15................today: January 16..............January 17 >>
Data4
Data5
Data6
When I click "January 17" data 4-6 should change to data 7-9 because data 7-9 is added to the MySQL database on January 17.
Of course I have a code to query the database and show today's data, but I am going crazy about not being able to reload the page with new data.
I tried to search the whole internet, but nothing I can find fits my needs.
Your question has a lot of sides to it. This answer's solely purpose to demonstrate how front end, back end and persistence layer communication works.
DO NOT USE THIS IN PRODUCTION! THIS CODE IS SHITTY AND FOR DEMO PURPOSES ONLY!
Modern web applications are not implemented like this. Front end frameworks/libraries are commonly used to keep complexity manageable.
First of all your back end should provide an API to the front end.
Lets assume your back end accepts the following HTTP GET request:
www.example.com/users/list_by_date/20150131
The last parameter is a date in YYYYMMDD format. Upon receiving this request, your back end sends a pre-rendered HTML snippet containing a list of users from that date. (Nowadays front end side rendering is quite popular and you should look into that topic as well. In this case your back end sends a list of users in JSON format which is then rendered on the front end side using templates. Adding code for this would make example even more complex).
In order to get this list of users, you initiate an AJAX request from the front end when a user clicks on a next-day button (which in your example contains "January 17"). Furthermore, you need to replace a list of currently shown users from January 16th with the new ones. Lets assume that list has a class attribute .users in HTML code. Here is a front end code using jQuery:
$(function() {
$(document).on('click', '.next-day', function() {
$.getJSON("/users/list_by_date/20150131", renderResults);
});
function renderResults = function(data) {
$('.users').html(data); // replaces old contents with the new ones received from server
};
});
You should also update prev/next link titles to the next dates, January 16th and January 18th respectively.
On the back end side, you execute a SQL statement to fetch a list of users. It could look something like this:
SELECT * from Users where DATE(datetimecolum) = '2015-01-17'
The exact way to put an attribute into a SQL query varies among programming languages/frameworks.
This should help you start working on your own solution. You probably should at least consider a front end framework/library to help you manage the complexity. You might want to spend a few days or even weeks getting familiar with front end technologies before attempting to implement a good solution on your own. It might be more cost efficient to hire a front end developer.
I did it on my own and the code actually were 5 lines.
I needed 2 days. Somebody who knows it, could have written it up in 2 minutes. So thanks for all the fish!
Here is the code, if somebody ever needs something like this:
The code for determining the very last date available (which I had already):
$query = mysqli_query($bd, "SELECT MAX(datum) as max_datum, MIN(datum) as min_datum FROM apps WHERE price = 0 ") or die(mysqli_error());
$maxdate = mysqli_fetch_assoc($query);
$maxdate = $maxdate['max_datum'];
$mindate = $mindate['min_datum'];
The actual code I was looking for to display two links for next and previous days (without design):
$date = isset($_GET['date']) ? $_GET['date'] : $maxdate;
$prev_date = date('Y-m-d', strtotime($date .' -1 day'));
$next_date = date('Y-m-d', strtotime($date .' +1 day'));
if ($date < $maxdate) {
echo "<a href=?date=$next_date>forward a day</a>";
}
if ($date > $mindate) {
echo "<a href=?date=$prev_date>back a day</a>";
}
The code executing the data which I had already:
$result = mysqli_query($bd, "SELECT * FROM apps WHERE datum = '".$date."' AND price = 0 ") or die(mysqli_error());
$count = mysqli_num_rows($result);
echo "<div id='icon-wrapper'>";
$cc = 0;
while ($row = mysqli_fetch_array($result)){
...
$cc++;
if ($cc < $count) {
echo "\n";
}
}
echo "</div>";
mysqli_close($bd);
?>
I have a wordpress toggle function that toggles a status depending on the value in the wp_database. The user basically has the option to report sick and to report healthy. When the user reports sick i want a piece of tekst to echo out the date stored in the database. So if for example i reported myself sick on the 1st of December 2014 i want to echo out that date. The code i have so far is listed below.
$date = $wpdb->get_results( "SELECT sick FROM ziekbeter WHERE person =
$user_ID AND healthy IS NULL" );
$status="You reported sick on DATE.";
Image being the 'ziekbeter' table in my database. I know for a fact that the array it echo's only contains a single date.
Lets say that we're person 2 and we take a look at my code above.
SELECT sick FROM ziekbeter WHERE person = $user_ID AND healthy IS NULL
This would select the 'sick' row from table 'ziekbeter' (the whole table) but only the value of 'sick' where person = $user_ID (which is the user thats currently logged in to the system) and when the 'healthy' field is empty. This value (which if we're person 2 is 2014-11-13) will get put into $date. Now the only problem i face is that i need to echo this date on my website. Is there any way to do this?
The second image is of my website's front-end. You see the big red button which says 'report healthy'. If the user clicks that butten the date which they enter in the date field on the right gets put in the database (in the healthy field). (NOTE: All of this code works, im just looking for a way to output my $date on my page).
EDIT
If i var_dump the array i get the following code:
array(1) { [0]=> object(stdClass)#2315 (1) { ["sick"]=> string(10)
"2014-11-03" } }
What
print_r($array);
tells you?
Did you try
echo $array["sick"];
?
or maybe
echo $array->sick ?
$array being the name of the array containing the result of your MySQL query (the one you var_dump'ed).
Ok turns out the answer was rather simple.
$date2 = $wpdb->get_results( "SELECT sick FROM ziekbeter WHERE person = $user_ID AND healthy IS NULL" );
foreach ($date2 as $row) {
echo $row->sick;
}
I simply had to declare he had to echo the sick field again. The original SELECT did select the table at first but didnt remember that value when stored in a variable. I added a foreach row in the date variable and echo'ed the sick field. Since there is only 1 value ever in the table it has my desired result. Thanks all!
I would need to create archives listing ( much the same functionality as the ones in WP websites ), but with using PHP and Yii framework. Since i've been thinking about how I could accomplish this a lot and didn't come up with anything useful, i'm asking your help here.
So for example, mysql table would look something like this:
Columns:
id (int AI, primary )
title ( varchar )
content ( text )
creation_date ( timestamp )
How could I use the timestamp column value, to select only the posts in distinct months posted ( so that I can determine which months I need to list as links to the respective archive page which will list all the posts posted under that month ) ?
Btw, only helping me on how to do it with PHP is enough, you don't have to go through the hussle of making the code work with Yii since, obviously it is not possible, because you dont have controllers, models etc. :)
Gj to whoever voted down my question, you were realy helpful. I guess haters exist even on these kinds of websites, which is sad. Anyways ,enough about them, here is something I came up with today by learning new MySQL function, which I didn't know existed before :)
You can just use MySQL DATE_FORMAT(date_column,'%format') to select the desired month, year, day, or any other "part" of the date, even though if the date is in unix timestamp. This is a simple code I came up with after a bit of fiddling around.
$query = "SELECT DISTINCT DATE_FORMAT(creation_date,'%M') as creation_month, DATE_FORMAT(creation_date,'%Y') as creation_year FROM posts";
$result = $connection->query($query);
if($result) {
while($row = $result->fetch_assoc()) {
$archiveArray[] = $row;
}
print_r($archiveArray);
echo '<h3>Our Archives by month</h3>';
echo '<ul>';?>
<?php foreach($archiveArray as $archiveRecord) : ?>
<li>
<?php echo $archiveRecord['creation_month'] . ' ' . $archiveRecord['creation_year']; ?>
</li>
<?php endforeach;
echo '</ul>';
}
What this code does is it "reads" the timestamp in the "creation_date" column from the DB, puts it inside the desired "format" which is specified by %M and %Y ( similar to PHP date() function formating) and assigned to immaginary column names on the fly called craetion_month and creation_year. Note the DISTINCT select, since I don't want months to repeat themselves for each post under that particular month. After that we just list them as anchors in an unordered list. This was the main problem, the rest is easy to do :)
Hello im building a multilenguaje app in php, this is the first time i handle multilenguaje, im trying to do it like this.
For months for example
i have a table "months" with id | month estructure
in the users table i have id | username | month_id
my guess (bad guess) its that i can save for example $january direct on the database so in the php when its called, it looked for the defined variable.
$january = "january"; and if im on spanish web then i change this to $january = "Enero";
but this is not working (obviusly because it prints as a result of a call $january, Its saved as $january in the database)
How is the best method for what im trying to do.. cause im lost in this point.. and i need this estructure in the database for a lot of reasons i wont mention, right now,
thanks!
My code
This $row2['months']
Print this $january
Even when in the php code i have set $january = "enero";
You might rethink your strategy.
Example:
For 10 languages if you keep 10 php files defining the month names or actually all language locale words in, it will be sufficient and you can include them without disturbing the database.
<?php
// this is words_en.php, you can make another like words_de.php etc.
$months = array('january','february','march','april'....etc );
?>
If you structure your locale file consistently for instance:
$msgs = array(
'MSG1'=>'first message',...
}
keeping only the references(like'MSG1') in your code and also in your database will be sufficient. For the months, you can keep them apart from $msgs since their use is specific and numeric indexing adds more consistency to coding for their case.
Note that this is not the only method for language locales but this is an easy to maintain version.
You should save the users locale e.g. en_US in your table and base further translations on that value with a fallback to the default language.
To get php to evaluate that variable, what you need to do is eval:
$january='Enero';
$row='$january';
echo $row; //Prints $january
echo PHP_EOL;
echo eval("return ".$row.";"); //Prints Enero
Here that is in ideOne: http://ideone.com/f0LCT0