'elseif value is less than getting value or greater than
I am trying to show echo 2 but its not working
$zipcode1 = 07300-011;
$zipcode= str_replace("-","",$zipcode1);
$zipcode = 07300011;
if ($zipcode >= 20000000 && $zipcode <= 26600999) {
echo '1';
}
elseif ($zipcode >= 07000001 && $zipcode <= 07399999) {
echo '2';
}
else {
echo 'no value';
}
Please let me know where i am doing wrong. Thank you for the help Result = 2
Solution 1:[1]
You made two mistakes.
One is that the value assigned to $zipcode1 is not a string, but rather the result of an arithmetic operation. I'm saying $zipcode1 is not "07300-011", but rather 07300-011, which is equal to the octal number 7300 (3776 in base 10) minus octal 11 (9 in base 10), i.e 3776 - 9 which is 3767.
The second is that you're trying to do a numeric comparison using strings. "20" > "1000" is not the same as 20 > 1000. The first would resolve to true, whereas the second would give false (which is likely what you want).
To fix this, you have to first convert both of them to numbers. You can either cast them:
((int) $zipcode1) > (int) $zipcode2
or you can use the + sign instead:
(+$zipcode1) > (+$zipcode2)
In both cases, you need to first remove whitespaces and every other non-numeric character from the zipcode.
$zipcode = str_replace([' ', '-'], '', $zipcode);
Read the following topics in the php docs for more info:
Solution 2:[2]
To answer on your example from the question - Pick the last element from your resultset:
link.find('span', attrs={'class':'views-field views-field-created'}).select('span')[-1].text
or shorter:
link.find_all("span")[-1].text
But if you want to extract all information and store as structured data, there would be a better approach with using stripped_strings.
Example
import requests
from bs4 import BeautifulSoup
url = 'https://web.archive.org/web/20150913224145/http://www.newyorksocialdiary.com/party-pictures'
res = requests.get(url)
soup = BeautifulSoup(res.text, "html.parser")
data = []
for item in soup.select('.view-content div'):
c = list(item.stripped_strings)
data.append({
'title':c[0],
'date':c[-1],
'url':item.a['href'].split('/',3)[-1]
})
print(data)
Output
[{'title': 'Kicks offs, sing offs, and pro ams', 'date': 'Friday, September 11, 2015', 'url': 'http://www.newyorksocialdiary.com/party-pictures/2015/kicks-offs-sing-offs-and-pro-ams'}, {'title': 'Grand Finale of the Hampton Classic Horse Show', 'date': 'Tuesday, September 1, 2015', 'url': 'http://www.newyorksocialdiary.com/party-pictures/2015/grand-finale-of-the-hampton-classic-horse-show'}, {'title': 'Riders, Spectators, Horses, and More ...', 'date': 'Wednesday, August 26, 2015', 'url': 'http://www.newyorksocialdiary.com/party-pictures/2015/riders-spectators-horses-and-more'}, {'title': 'Artist and Writers (and Designers)', 'date': 'Thursday, August 20, 2015', 'url': 'http://www.newyorksocialdiary.com/party-pictures/2015/artist-and-writers-and-designers'}, {'title': 'Garden Parties Kickoffs and Summer Benefits', 'date': 'Monday, August 17, 2015', 'url': 'http://www.newyorksocialdiary.com/party-pictures/2015/garden-parties-kickoffs-and-summer-benefits'}, {'title': 'The Summer Set', 'date': 'Wednesday, August 12, 2015', 'url': 'http://www.newyorksocialdiary.com/party-pictures/2015/the-summer-set'}, {'title': 'Midsummer Parties', 'date': 'Wednesday, August 5, 2015', 'url': 'http://www.newyorksocialdiary.com/party-pictures/2015/midsummer-parties'}, {'title': 'The Watermill Center and The Parrish', 'date': 'Wednesday, July 29, 2015', 'url': 'http://www.newyorksocialdiary.com/party-pictures/2015/the-watermill-center-and-the-parrish'}, {'title': 'Unconditional Love', 'date': 'Thursday, July 23, 2015', 'url': 'http://www.newyorksocialdiary.com/party-pictures/2015/unconditional-love'}, {'title': "Women's Health, Boys & Girls, Cancer Research, and Just Plain Summer Fun", 'date': 'Friday, July 17, 2015', 'url': 'http://www.newyorksocialdiary.com/party-pictures/2015/womens-health-boys-girls-cancer-research-and-just-plain-summer-fun'},...]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Praise Dare |
| Solution 2 | HedgeHog |
