How to if elseif correctly with current SMARTY code? - smarty

I was converting everything from our existing bash scripts to use our new configuration. So after spending an hour, I noticed the bash scripts weren't even used! I noticed this after making changes didn't work. * facepalm*
Anyways, apparently the interface we use is based mainly on SMARTY templates. Now I want to create several rules, but I am not sure on the proper formatting. Maybe someone can jump in and advice me how to format it correctly?
The original piece of code is this:
if (($data['mem'] != $_SESSION['vps'][$data['veid']]['orig']['memory']) && ($data['mem'] > 0))
{
$parameter .= " --kmemsize ".($data['mem']*203636).":".($data['mem']*224000);
$query[] = "memory=".$data['mem'];
}
Now what I want to do is something like this:
$mem < 768
then this should happen:
$parameter .= " --kmemsize ".($data['mem']*2120000).":".($data['mem']*2140368);
$mem < 2048
then this should happen:
$parameter .= " --kmemsize ".($data['mem']*706672).":".($data['mem']*727040);
else:
$parameter .= " --kmemsize ".($data['mem']*203636).":".($data['mem']*224000);
How do I apply that based on the original code above?
Maybe someone can give me a workable example which I can use to apply it to the above code and create all the rules I need.
I am a bit worried, if I use wrong pieces of code, that I will mess up things. This is something I don't want obviously.
Thanks in advance.

Related

socket.io send an object to a html table

I am a real newbie - I was given a leg up to create something and it is using socket.io. I'm trying to fiddle around the edges, learning odd things as I go now. But have hit a wall in a really early stage of my project. It May well be too deep ... but just in the case asking here gives me an answer I can make sense of - here goes:
I have essentially three files working together. server.js, client.js, and index.ejs (which is my html file). I have a series of stuff happening in a "room" and now I want to display the values (next I will want to do more with them, but for now display) of an object in a html table.
server.js
... it creates the information of interest (uses a database call that currently works) and then:
io.to(roomId).emit('room-location-update', result.rows);
client.js
... receives data. My console.log has it all there. Then I assign a variable to hold it for use in the html:
socket.on("room-location-update", (data_information) => {
console.log(data_information);
//I think I need code in here?
var wp = data_information;
}
index.ejs
... fails to show anything. I have a table constructed to use the variable from client.js in a series of table cells essentially all constructed as:
<td id=wp.name><td id=wp.radius>
but nothing is displayed. the .name and .radius attributes are valid in the database when data_information is first created.
why nothing displayed? Google searches imply (to me) that this is basic stuff and should work. So clearly I am missing something basic (?) Any ideas what?
sorry, I don't actually know what you are talking about with edits. Apologies if I have done something wrong - I am a genuine newbie.
In the end I gave up on doing it in the ejs file and created a big string containing the html table codes and substituting in values where I wanted them in the client.js. And just popped that into the ejs file. No idea if this is efficient or not but it works, so hooray.
truncated code snippet of what worked for me (in client.js):
var $table = "<table border='1'>"
$table += "<thead><tr><th>Player</th><th>Location</th></tr></thead><tbody>"
for (var i = 0; i < display_information.length; i++) {
$table += '<tr><td>' + display_information[i].id + '</td>'
$table += '<td>' + display_information[i].name + '</td>'
}
$table += "</tr></tbody></table>"
$('#displayinfo').empty().append($table);
and then in my index.ejs is: <pre><span id="displayinfo"></span></pre>

Get first letter of word in statement

I have a statement like "Animal Association" from the database. I want to get its short form. It means, only the first letter of each word like this "AA". In the blade file, I got the whole statement as follows,
<p>{{ $animal->user->club->name}}</p>
So, how can I get a short form of this name?
Thank You!
If you are using MySQL 8+, then a raw select with REGEXP_REPLACE should work here:
$users = DB::table('animals')
->select(DB::raw("SELECT REGEXP_REPLACE(name, '(\\w)\\w+\\s*', '$1')"))
->get();
This very common problem where we ran into, I can provide you a function that will solve your problem. I am sharing two solutions and you can use any of these solutions.
using function
You can use this function in your model and solve your problem.
public function getNameAbbreviate($string){
$abbreviation = "";
$string = ucwords($string);
$words = explode(" ", "$string");
foreach($words as $word){
$abbreviation .= $word[0];
}
return $abbreviation;
}
There is probably no one-line solution, the solution which I provided is readable and understandable.
using regex
This solution is easy to apply and in case you can't make the first method work then go with this.
<p>{{ preg_split("/\s+/", $animal->user->club->name) }}</p>
using regex we can get a direct solution but I personally don't like it or recommend it.

Subtract 2 random numbers, unable to compare

I'm new to coding so it's probably a stupid mistake but I just can't figur out what is going wrong. I made an email form with a function to check if it's a human sending the message.
They have to answer a simple math question generated with random numbers.
But when I try to check if the input is correct, something goes wrong.
The relevant parts of the code I'm using:
First I generate two random numbers, calculate the correct outcome and turn the answer into a variable:
$number = array( mt_rand(1,5), mt_rand(6,10) );
$outcome = $number[1] - $number[0];
$human = $_POST['message_human'];
The part where to put the answer:
<label for="message_human">
<input type="text" style="width: 44px;" placeholder="…" name="message_human"> + <?php echo $number[0];?> = <?php echo $number[1];?>
</label>
The part to check if the answer is correct, and perform action:
if(!$human == 0){
if($human != $outcome) my_contact_form_generate_response("error", $not_human); //not human!
else { //validate presence of name and message
if(empty($message)){
my_contact_form_generate_response("error", $missing_content);
} else { //ready to go!
$message = "http://url.com" . $url . "\n\n" . $message;
$sent = wp_mail($to, $subject, strip_tags($message), $headers);
if($sent) my_contact_form_generate_response("success", $message_sent);
else my_contact_form_generate_response("error", $message_unsent);
}
}
} else if ($_POST['submitted']) my_contact_form_generate_response("error", $missing_content);
I keep getting the "not human error".
I tried making $outcome an array and all kind of operators but nothing works.
When I give the $outcome a fixed value like = "2"; everything works fine, but I want it to be a random number. Some help would be much appreciated.
If I understand your code right, you are not saving those random numbers, right?
So how could you get the correct comperison, when you send the answer to a previous generated set of random numbers when you just generate new ones?
A possible solution may be to save those values in a session variable.
session_start();
$_SESSION['outcome'] = $outcome;
and compare it later with this variable, but make sure it is not overwritten by a new generated set of random numbers.
if($_POST['message_human'] == $_SESSION['outcome']){
//correct
}

xpaths - retrieve text inside attribute and split / preg_replace?

I have something that looks like this:
<strong class="citizen_level" title="<strong>Experience Level</strong><br/>1,137,935 / 1,140,000">252</strong>
I managed to extract 252 using :
//div[#id='content']/div[#class='citizen_profile_header']/h2/strong
But I also want to extract 1,137,935, but they have put that inside a title attribute, which I should be able to extract by using
//div[#id='content']/div[#class='citizen_profile_header']/h2/strong/#title
Which should output
<strong>Experience Level</strong><br/>1,137,935 / 1,140,000
Is there any reason the above code wouldnt work ?
And can someone help me here, and get this down to 1137935 ? What would be the best way ?
Edit:
$string = "<strong>Experience Level</strong><br/>1,137,935 / 1,140,000";
$tmp = preg_replace('/[^0-9\/]/',"",$string);
$tmp = ltrim($tmp, '//');
$tmp = preg_split("/[\/,]+/", $tmp);
$string = reset($string);
Maybe not the best way, but it seems to work ? But for some reason
//div[#id='content']/div[#class='citizen_profile_header']/h2/strong/#title
doesn't seem to work =/

Rewrite rules in the .htaccess file

The request is simple, however, I cannot find a way to implement it. I have links like:
httр://mysite.com/index.php?lang=EN
httр://mysite.com/index.php?route=add&lang=EN
httр://mysite.com/index.php?route=view&lang=EN
and so on. What I want is to create 301 redirects so that EN could be changed to GB. For example, if a customer opens httр://mysite.com/index.php?route=add&lang=EN, he should be redirected to httр://mysite.com/index.php?route=add&lang=GB.
I have searched for this for days and have failed to find a working solution. Please help.
Does it have to be done in .htaccess? Here's a relatively simple way of doing it in PHP:
<?
if ("EN" == $_GET['lang']) {
$params = $_GET;
$params['lang'] = "GB";
$query_strings = array();
foreach ($params as $key => $value) {
$query_strings[] = $key . "=" . $value;
}
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.mysite.com?" . join($query_strings, "&");
}
Bottom line is that it may be easier to fix this problem on a level where you can isolate each query parameter and look at just the lang parameter and determine whether to do a redirect.
With regular expressions (as you would need to use in .htaccess) it's harder to isolate just the lang part. You would also need one line per language you want to redirect and maintain the list.

Resources