How to join the same column more than once and show it in a page - join

I want to join the same column three times then display each different value in my page.
I hope someone can find the problem. It shows the same language three times and it also shows the third one not the first one.
[My Query][1]
<?php
include '../config.php';
$id=$_REQUEST['id'];
$sql = " SELECT *
FROM master_student_info
INNER JOIN master_streams a ON master_student_info.stream_id = a.mst_stream_id
INNER JOIN master_streams b ON master_student_info.stream2_id = b.mst_stream_id
INNER JOIN master_streams c ON master_student_info.stream3_id = c.mst_stream_id
INNER JOIN master_schedules d ON master_student_info.schedule_id = d.mst_schedule_id
INNER JOIN master_schedules e ON master_student_info.schedule2_id = e.mst_schedule_id
INNER JOIN master_schedules f ON master_student_info.schedule3_id = f.mst_schedule_id
INNER JOIN master_cities ON city_id = mst_city_id
WHERE id = ".$_GET['id'];
$res=mysqli_query($con,$sql) or die(mysqli_error($con));
$th=mysqli_fetch_assoc($res);
?>
[master_student_info table][2]
id, name, email, mobile, dob, fname, fmobile, mname, mmobile, city_id, address, stream_id, stream2_id, stream3_id, schedule_id, schedule2_id, schedule3_id, doa, picture, how_id, cbox1, cbox2
[master stream table][3]
mst_stream_id, mst_stream_name
[master stream fields][4]
mst_stream_id mst_stream_name
1 English
2 German
3 Japanese
4 French
5 Italian
[my code][5]
<li class="list-group-item">
<b> Language 1</b> <a id="stream_id" class="pull-right" > <?= $th['mst_stream_name']?> </a>
</li>
<li class="list-group-item">
<b> Language 2</b> <a id="stream2_id" class="pull-right" > <?= $th['mst_stream_name']?> </a>
</li>
<li class="list-group-item">
<b> Language 3</b> <a id="stream3_id" class="pull-right" > <?= $th['mst_stream_name']?> </a>
</li>
[master student info example][7]
id = 1
name = name
stream_id = 1
stream2_id = 2
stream3_id = 3

Related

Select all tag (and extract text) between two other tags with BeatifulSoup

I would like to extract all instances of a given tag that are included between two tags. Currently I am working with BeautifulSoup.
Below you can find an example:
<p class='x' id = '1'> some content 1 <p>
<p class='y' id = 'a'> some content a <p>
<p class='y' id = 'b'> some content b <p>
<p class='y' id = 'c'> some content c <p>
<p class='potentially some other class'> <p>
<p class='x' id = '2'> some content 2 <p>
<p class='y' id = 'd'> some content d <p>
<p class='y' id = 'e'> some content e <p>
<p class='y' id = 'f'> some content f <p>
I am interested in selecting all the instances of class 'y' between the two tags 'x', which also have different id's. With regards to the specific example, I would like to select all p's with class = 'y' to then retrieve the text. My final desired output would be: 'some content a', 'some content b', and 'some content c'.
I tried using the findAllNext method, but that gives me 'some content a', 'some content b', 'some content c' AND 'some content d', 'some content e', 'some content f'.
Below is my code
par = BeautifulSoup(HTML_CODE).content, 'lxml')
loc = par.find('p', class_ = 'x', id ='1')
desired = loc.findAllNext('p', class_ = 'y')
Is there any way to avoid selecting also the instances of class = 'y' that appear after the tag with class='x' with id = '2' ?
Thank you.
You can start iterating from where you want and end it until found something mark finished.
from bs4 import BeautifulSoup
html = """
<p class='x' id = '1'> some content 1 </p>
<p class='y' id = 'a'> some content a </p>
<p class='y' id = 'b'> some content b </p>
<p class='y' id = 'c'> some content c </p>
<p class='potentially some other class1'> potentially some other class 1 </p>
<p class='potentially some other class2'> potentially some other class 2</p>
<p class='potentially some other class3'> potentially some other class 3 </p>
<p class='x' id = '2'> some content 2 </p>
<p class='y' id = 'd'> some content d </p>
<p class='y' id = 'e'> some content e </p>
<p class='y' id = 'f'> some content f </p>
"""
soup = BeautifulSoup(html,"lxml")
start = soup.find("p",class_="y",id="c")
end = soup.find("p",class_="x",id="2")
def next_ele(ele,result=[]):
row = ele.find_next("p")
if not row or row == end:
return result
result.append(row)
return next_ele(row,result)
print(next_ele(start))

Combine values of span classe and title attribute

Cant figure out how to get a combination of the span attribute 'title' and the text value of the span class num
<ul>
<li>
<span class="abc" title="HOUSES"> </span>
<span class="num">1</span>
</li>
<li>
<span class="def" title="CARS"> </span>
<span class="num">2</span>
</li>
<li>
<span class="ghj" title="AGE"> </span>
<span class="num">90</span>
</li>
</ul>
How can i get attributes like ?
HOUSES = 1
CARS = 2
AGE = 90
This is where i am but not solving the problem so far
for li_tag in soup.find_all('ul'):
for span_tag in li_tag.find_all('li'):
for span in span_tag.find_all('span'):
print(span)
This is how can try to get the desired results:
from bs4 import BeautifulSoup
content = """
<ul>
<li>
<span class="abc" title="HOUSES"> </span>
<span class="num">1</span>
</li>
<li>
<span class="abc" title="CARS"> </span>
<span class="num">2</span>
</li>
<li>
<span class="abc" title="AGE"> </span>
<span class="num">90</span>
</li>
</ul>
"""
soup = BeautifulSoup(content,"lxml")
for items in soup.find_all("li"):
title = items.find("span").get("title")
number = items.select_one("span:nth-of-type(2)").text
print("{} = {}".format(title,number))
you can try like this as well:
for items in soup.find_all(class_="num"):
title = items.find_previous_sibling()['title']
number = items.text
print("{} = {}".format(title,number))
This is another way:
for items in soup.select("[title]"):
title = items.get("title")
number = items.find_next().text
print("{} = {}".format(title,number))
Or like this:
for items in soup.find_all(lambda e: e.get("title")):
title = items.get("title")
number = items.find_next_sibling().text
print("{} = {}".format(title,number))
Output:
HOUSES = 1
CARS = 2
AGE = 90

python beautifulsoup extract text

I am trying to extract Manufacturer # and PAW11295 from below html source code and got stuck. Appreciate any suggestions.
soupTest.find("div",id = "AddnInfo")
Out[121]:
<div id="AddnInfo">
<h3>Additional Info</h3>
<p>
<p class="sknText"><label>“R”Web#:</label> <span class="value">215904</span> </p>
<p class="skuText"><label>SKU:</label> <span class="value">B7958C02</span> </p>
<p class="upc"><label>UPC/EAN/ISBN:</label> <span class="value">092317112958</span></p>
<p><label>Manufacturer #:</label> PAW11295</p>
<p><label>Product Weight:</label>2.2 pounds</p>
<p><label>Product Dimensions (in inches):</label>12.7 x 10.1 x 5.4</p>
</p>
</div>
Thanks in advance.
The following approach should work. It takes the 5th <p> element and gets the <label> text. It then removes this and display the stripped text for the whole <p> tag:
from bs4 import BeautifulSoup
html = """
<div id="AddnInfo">
<h3>Additional Info</h3>
<p>
<p class="sknText"><label>“R”Web#:</label> <span class="value">215904</span> </p>
<p class="skuText"><label>SKU:</label> <span class="value">B7958C02</span> </p>
<p class="upc"><label>UPC/EAN/ISBN:</label> <span class="value">092317112958</span></p>
<p><label>Manufacturer #:</label> PAW11295</p>
<p><label>Product Weight:</label>2.2 pounds</p>
<p><label>Product Dimensions (in inches):</label>12.7 x 10.1 x 5.4</p>
</p>
</div>
"""
soup = BeautifulSoup(html)
div = soup.find('div', {'id':'AddnInfo'})
p = div.find_all('p')[4]
label = p.find('label')
manufacturer = label.text
label.extract()
id = p.get_text(strip=True)
print manufacturer
print id
Which would display:
Manufacturer #:
PAW11295
I think you want something like this.
First select the outer P tag. Then select all the inner P tags. Then reference the individual P tag you want, in this case the fourth one.
infoDiv = soupTest.find("div",id = "AddnInfo")
outerPs = infoDiv.p # isolate the outer <P>
innerPs = outerPs.find_all('p') # returns a list of the inner <P>s
manufacturer_number = innerPs[3].string # you will have to trim the <label>
manufacturer_code = innerPs[3].label.string # will need trimming

Select text from multiple sub nodes in an xpath

I need to use XPath with lxml in Python 2.6 to extract two text items:
-Name One Type 1 Description 1
-Name Two Type 2 Description 2
I've tried using the following Xpath: '//*[#id="results"]/li/div/p/child::text()'
However this gives me only the following text
-Name One Type 1
-Name Two Type 2
Any suggestions on the correct Xpath to use?
<div id="container">
<ol id="results">
<li class="mod1" data-li-position="0">
<img src="image001.jpg">
<div class="bd">
<h3>
Category 1
</h3>
<p class="description">
<strong class="highlight">Name One</strong>
<strong class="highlight">Type 1</strong>
Description 1
</p>
</div>
</li>
<li class="mod2" data-li-position="1">
<img src="image002.jpg">
<div class="bd">
<h3>
Category 2
</h3>
<p class="description">
<strong class="highlight">Name Two</strong>
Description 2
<strong class="highlight">Type 2</strong>
</p>
</div>
</li>
This last part of your XPath :
...../p/child::text()
... select only text nodes which is child of child of <p>. That's why you missed, for example, Description 1, because it is direct child of <p>. You can try to change that part to be as follow :
...../p//text()
Above XPath will select all text nodes which are descendants of <p>, in other words, all text nodes anywhere within <p>.

Grouping in Linq in a not so well organized xml

Im using YQL to get some data and here is my xml:
<?xml version="1.0" encoding="UTF-8"?>
<div id="content>
<div id="html">
<h3>City</h3>
<div id="movie">
<h4>
movie 1
</h4>
<div>
<div id="movie>
<h4>
movie 2
</h4>
</div>
.
.
.
<h3>City 2</h3>
<div id="movie">
<h4>
movie 1
</h4>
<div>
<div id="movie>
<h4>
movie 2
</h4>
</div>
I want to populate a listbox in my windows phone app with the city name and its movies like this:
City 1
Movie1
Movie 2
City 2
Movie1
Movie 2
However, I'm getting stuck here since all the XML is inside the div with id content.
How could a make a LINQ statement to solve this?
I tried to morph your posted input into some well-formed XML:
<div id="content">
<div id="html">
<h3>City</h3>
<div id="movie">
<h4>
movie 1
</h4>
</div>
<div id="movie">
<h4>
movie 2
</h4>
</div>
.
.
.
<h3>City 2</h3>
<div id="movie">
<h4>
movie 1
</h4>
</div>
<div id="movie">
<h4>
movie 2
</h4>
</div>
</div>
</div>
Then you can get a List with the code
XDocument doc = XDocument.Load("input.xml");
List<string> data =
doc.Descendants("h3")
.Union(
doc.Descendants("div")
.Where(d => (string)d.Attribute("id") == "movie")
.Elements("h4")
.Elements("a")
).InDocumentOrder()
.Select(e => e.Value)
.ToList();
[edit] Your initial request seemed to ask for a flat list result, your comment suggests you rather want a grouped structure so here is an adapted sample:
XDocument doc = XDocument.Load("input.xml");
var groupedData =
(from movie in doc.Root.Descendants("div")
where (string)movie.Attribute("id") == "movie"
group movie by movie.ElementsBeforeSelf("h3").Last() into g
select new
{
city = g.Key.Value,
movies = (from m in g
select (string)m.Element("h4").Element("a")).ToList()
}).ToList();
// now use above list for data binding or
// in the simplest case just consume it with foreach:
foreach (var group in groupedData)
{
Console.WriteLine("city: {0}:", group.city);
foreach (var movie in group.movies)
{
Console.WriteLine(movie);
}
}

Resources