Viewed   80 times

Here is my PHP code with SQL query, but the output isn't as expected:

$sql = 'INSERT INTO `event_footers` (`event_id`, `order`, `file_id`, `url`) VALUES ';
foreach($all_footers as $key => $val){
    $sql .= '('.(int)$data['event_id'].', '.$key + 1 .', '.(int)$val['file_id'].', "'.addslashes($val['url']).'"), ';
}

$sql = rtrim($sql, ', ');
var_dump($sql);
exit;

AND I get sql query like this:

`INSERT INTO `event_footers` (`event_id`, `order`, `file_id`, `url`) VALUES 1, 2135, "http://11.lt"), 1, 2136, "http://22.lt"), 1, 2140, "http://44.lt")`

Where is the first ( after VALUES?

 Answers

2

+ and . have the same operator precedence, but are left associative. Means after the first concatenation:

'(' . (int)$data['event_id']

The string got added with your key, e.g.

"($data['event_id']" + $key

So the string gets converted into an integer in that numerical context and disappears. To solve this use parentheses () around your addition.

Friday, October 21, 2022
1

Just use . for concatenating. And you missed out the $personCount increment!

while ($personCount < 10) {
    $result .= $personCount . ' people';
    $personCount++;
}

echo $result;
Thursday, September 22, 2022
4

Your code, as written, works. You’re probably trying to achieve something unrelated, but similar:

std::string c = "hello" + "world";

This doesn’t work because for C++ this seems like you’re trying to add two char pointers. Instead, you need to convert at least one of the char* literals to a std::string. Either you can do what you’ve already posted in the question (as I said, this code will work) or you do the following:

std::string c = std::string("hello") + "world";
Thursday, December 8, 2022
 
4

The . operator is the concatenation operator. Your first example only works because the echo 'function' (technically it's a language construct, but lets not split hairs) accepts more than one parameter, and will print each one.

So your first example is calling echo with more than one parameter, and they are all being printed, vs. the second example where all the strings are being concatentated and that one big string is being printed.

Friday, September 9, 2022
 
ow3n
 
2

Django's url resolvers only work on current language. So you will need to switch language before attempting to solve an url in a specific language, using translation.activate.

For resolving the url, that means you must know the language beforehand, switch to it and only then resolve (basically what the localemiddleware will do for you).

For reversing the url, that means you should probably reverse the url using its name. You'll get back the url in current language. I cannot test right now, but it should work like this:

from django.utils import translation
translation.activate('fr')
reverse('produits_index')    # /fr/produits/
translation.activate('en')
reverse('produits_index')    # /en/products/

If you did manage to get a ResolverMatch object, you have the url name as an attribute on it, conveniently named url_name.

I hope it helps, I am a bit unclear as to what you are trying to do. Feel free to comment/edit your question and I'll try to update this answer.


Update by Olivier Pons


Here's the working solution:

here's my working solution, which is close to spectras, but works the way I wanted:

# (!) resolve() use current language
#     -> try to guess language then activate BEFORE resolve()
lg_curr = translation.get_language()
lg_url = get_language_from_path(url) or lg_curr
translation.activate(lg_url)
try:
    resolve(url)
    req.session['url_back'] = url  # no error -> ok, remember this
except Resolver404:
    pass
translation.activate(lg_curr)

...and then later on, after successful registration/login, if there's a req.session['url_back'] then I remove it from session and make a redirect on it.

Friday, September 2, 2022
 
daxur
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :