This is the output of one of my file
<table>
<tr>
<td> Description 1 </td> <td> Content 1 </td>
</tr>
<tr>
<td> Description 2 </td> <td> Content 2 </td>
</tr>
<tr>
<td> Description 3 </td> <td> Content 3 </td>
</tr>
I want to get the content of Description x with php and then I want to read out the Content x. One of my problem is, that before grapping the content I don't know how many description-td's I have.
Should DOM / getElementsBy ... work for my problem?
Best regards,
Susan
Here you have a solution with JQUERY. Try if yourself
In this example we show in an alert dialog the content of the description number 3.
HTML:
<table id="mytable">
<tr>
<td> Description 1 </td> <td> Content 1 </td>
</tr>
<tr>
<td> Description 2 </td> <td> Content 2 </td>
</tr>
<tr>
<td> Description 3 </td> <td> Content 3 </td>
</tr>
</table>
JQUERY:
// Search content of description number 3
$(document).ready(extractContent(3));
function extractContent(num) {
$('td', $('#mytable')).each(function() {
// The text in the first column must match " Description num "
if ($(this).text() == " Description " + num + " ")
alert("Content found: "+$(this).next().text());
});
}
Related
This data is showing in MySql Database
<table>
<tr>
<td>
Team
</td>
<td>
Score
</td>
</tr>
<tr>
<td>
New Zealand
</td>
<td>
10
</td>
</tr>
<tr>
<td>
2
</td>
<td>
Australia
</td>
<td>
5
</td>
</tr>
<tr>
<td>
New Zealand
</td>
<td>
5
</td>
</tr>
</table>
and I want to show data on webpage like this
<table>
<tr>
<td>
Team
</td>
<td>
Score
</td>
</tr>
<tr>
<td>
New Zealand
</td>
<td>
15
</td>
</tr>
<tr>
<td>
2
</td>
<td>
Australia
</td>
<td>
5
</td>
</tr>
</table>
Note that in database "New Zealand" showing 2 time in Database but on webpage it showing 1 time and also its score in sum
please tell me how can I do this using mysqli
Thanks in Advance.
Without your query I can't use your table or field names, but it would look something like this:
SELECT `team`, SUM(`score`) AS `score_sum` FROM `teams` GROUP BY `team`
This groups the rows by team and for each group, the scores are summed for that team. You would then access the team name using team and the total score for each team using score_sum in your PHP code. You'll have to provide more details like your query and code if this doesn't help.
I'll give a suggestion for your table , considering that you have already created soccer table with three columns ID,Team,Score . regardless a way you insert a data in this table , here how to get it properly
<?php
include("connection.php");
$team = "select distinct team from soccer";
$team2 = mysqli_query($mysqli,$team);
$up = "0";
$id ="1";
echo "<table><tr><td>ID</td><td>TEAM</td><td>SCORE</td></tr><tr>";
while ($teamres = $team2->fetch_array()){
echo "<td>".$id."</td><td>".$teamres['team']."</td>";
$score = mysqli_query($mysqli,"SELECT sum(score) FROM soccer where team ='".$teamres['team']."'");
$score2 = mysqli_fetch_row($score);
$scorere = $score2[0];
echo "<td>".$scorere."</td>";
echo "</tr>";
$id++;
$up++;
}
echo "</table>";
?>
I'm working with PHP and MySQL on a student registration project.
There is a table named programme from which I have to find the value of total no. of registration in every campus.
There are 4 campuses (Delhi, Noida, Jaipur and Mumbai) and 27 courses with 2 date of exam cycle (12-04-2014 and 07-06-2014).
I have to display total no. of registration in every campus for every course in every campus. for e.g. Delhi campus I made this query for first date:
$sql="select * from programme where campus1='delhi' && course1='Fashion Design (FD)' && examdate='12-04-2014'";
$result=mysql_query($sql);
$delhi=mysql_num_rows($result);
and it does echoe the result into the td for the first cycle.
But now I have to make a single query for 27 courses.
I've thought to do this via switch cases, but am unable to do it. How should I proceed?
Here is the Table format which I have to fill from database; it's for one course and similarly there are 26 other courses:
<table class="table table-bordered">
<tr>
<th> </th>
<th colspan="10">Course Wise Registration Report</th>
<th> </th>
</tr>
<tr>
<th>PROGRAMME</th>
<th colspan="2">Delhi</th>
<th colspan="2">Noida</th>
<th colspan="2">Jaipur</th>
<th colspan="2">Mumbai</th>
<th colspan="2">Cycle</th>
<th>TOTAL</th>
</tr>
<tr>
<td> </td>
<td>Cycle-1</td>
<td>Cycle-2</td>
<td>Cycle-1</td>
<td>Cycle-2</td>
<td>Cycle-1</td>
<td>Cycle-2</td>
<td>Cycle-1</td>
<td>Cycle-2</td>
<td>Cycle-1</td>
<td>Cycle-2</td>
<td> </td>
</tr>
<tr>
<td>UG-FD</td>
<td><?php echo $delhi; ?></td>
<td></td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>UG-CD</td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>UG-TD</td>
------------
</tr>
UG-JD
------------
<th>Total</th>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
</table>
Here is DB table structure:
examdate campus1 course1 campus2 course2
Here is the format which i want :
Here is the screenshot of DB table programme:
You need to create a query something below.
SELECT COUNT(*) FROM programme
WHERE campus1 = 'delhi' AND examdate = '12-04-2014'
GROUP BY course1
Listen! this is not the final solution. Your tables structures seems not well organized. It should go through several normalization process. Until I have your complete set of table structure I can not give you a final answer.
For now.. just copy and run the above query in your PhpMyAdmin and see what it produce, then you will get an idea.
NOTE : I think it is not possible for you to explain well enough, if so, put some screenshots of your all tables grabbed form PhpMyAdmin and screenshot of your final result how it should looks like.
The query you want is:
SELECT campus1
,course1
,examdate
,count(*) AS count
FROM programme
WHERE examdate IN ('12-04-2014','07-06-2014')
GROUP BY campus1,course1,examdate
You may want to expand the WHERE clause if you don't want to include all campuses and courses.
here different solutions for ur problem
in the main page use combo and selecting campus, list details of that campus below that
combo using a ajax page
combo posting page you just receive the value of that combo means compus name and add to
ur sql directly.
If we use switch we want to enter a input to switch. depending on this input we can
change the output in each case.
lets say i retrieve all of the values where their position belongs to top8.I populate them out in a table and instead of displaying different kinds of values , it displays 3 tables with 3 different values, how is this so? any help so that different values belonging to certain values will all be displayed out? i only need one table with 3 different values.
<?
$facebookID = "top8";
mysql_connect("localhost","root","password") or die(mysql_error());
mysql_select_db("schoutweet") or ie(mysql_error());
$data= mysql_query("SELECT schInitial FROM matchTable WHERE position='".$facebookID."'")
or die(mysql_error());
while($row = mysql_fetch_array($data))
{
?>
<center>
<table border="0" cellspacing="0" cellpadding="0" class="tbl_bracket">
<tr>
<td class="brack_under cell_1"><a href="www.facebook.com"/>team 1.1><?= $row['schInitial']?><a/></td>
<td class="cell_2"> </td>
<td class="cell_3"> </td>
<td class="cell_4"> </td>
<td class="cell_5"> </td>
<td class="cell_6"> </td>
</tr>
<tr>
<td class="brack_under_right_up">team 1.2><?= $row['schInitial']?></</td>
<td class="brack_right"><!--1.2.1--></td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td class="brack_right"><!--2.1--></td>
<td class="brack_under"><!--3.1--></td>
<td><!--here?--></td>
<td><!--there?--></td>
<td><!--everywhere?--></td>
</tr>
</table>
</center>
<?
}
?>
</body>
That's because your <table> tag is within the loop! Place the <table> tag outside the while loop.
place your table tags outside the while loop
Because your writing the table tag inside the while loop. Everything inside the loop is done each loop cycle. If you only want to have one table in the output, you'll have to open and close the table outside of the loop, like this:
$data= mysql_query("SELECT schInitial FROM matchTable WHERE position='".$facebookID."'")
or die(mysql_error());
?>
<center>
<table border="0" cellspacing="0" cellpadding="0" class="tbl_bracket">
<?
while($row = mysql_fetch_array($data))
{
?>
<tr>
<td class="brack_under cell_1"><a href="www.facebook.com"/>team 1.1><?= $row['schInitial']?><a/></td>
<td class="cell_2"> </td>
<td class="cell_3"> </td>
<td class="cell_4"> </td>
<td class="cell_5"> </td>
<td class="cell_6"> </td>
</tr>
<tr>
<td class="brack_under_right_up">team 1.2><?= $row['schInitial']?></</td>
<td class="brack_right"><!--1.2.1--></td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td class="brack_right"><!--2.1--></td>
<td class="brack_under"><!--3.1--></td>
<td><!--here?--></td>
<td><!--there?--></td>
<td><!--everywhere?--></td>
</tr>
<?
}
?>
</table>
</center>
That will, however, print three rows per loop and therefore per record (but you have references to the table contents in two of them, so I suppose that's what you want?).
Also take care about some not well-formed HTML you have there (e.g. the > character in the expression team 1.1> / team 1.2>. If you want to print the > character to the browser, encode it as HTML entity (> for this case). You also have a probably superfluous </ in the first column of the second row (</</td>).
you need to echo the HTML part as well in the while loop like
echo '<table>';
My xpath:
(//tr[td[contains(., 'Refine by Vehicle Types')]])[1] /following-sibling::tr /td/div/table /tr/td/font /ul/li/a
My source:
<tr><td><font color="White">Refine by Vehicle Types</font></td> </tr>
<tr><td><div>
<table> <tr> <td><font<ul><li><a> Automobile/Light Trucks</a></li></ul></font></td> </tr> </table>
</div></td> </tr>
<tr> <td></td> </tr>
<tr> <td><font>Refine by Category</font></td> </tr>
<tr> <td><div>
<table> <tr> <td><font><ul><li><a>Agricultural</a></li></ul></font></td></tr>
I'm trying to scrape this source and collect the <li> nodes after "Refine by Vehicle Types" but not after "Refine by Category".
Any help is appriciated.
You are almost there.
Change:
(//tr
[td[contains(., 'Refine by Vehicle Types')]]
)
[1]
/following-sibling::tr
/td/div/table
/tr/td/font
/ul/li/a
to:
(//tr
[td[contains(., 'Refine by Vehicle Types')]]
)
[1]
/following-sibling::tr[1]
/td/div/table
/tr/td/font
/ul/li/a
When the second XPath expression is evaluated against the following XML document (your severely malformed text corrected to become a well-formed XML document):
<table>
<tr>
<td>
<font color="White">Refine by Vehicle Types</font>
</td>
</tr>
<tr>
<td>
<div>
<table>
<tr>
<td>
<font>
<ul>
<li>
<a> Automobile/Light Trucks</a>
</li>
</ul>
</font>
</td>
</tr>
</table>
</div>
</td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td>
<font>Refine by Category</font>
</td>
</tr>
<tr>
<td>
<div>
<table>
<tr>
<td>
<font>
<ul>
<li><a>Agricultural</a></li>
</ul>
</font>
</td>
</tr>
</table>
</div>
</td>
</tr>
</table>
Only one -- the wanted -- a element is selected:
<a> Automobile/Light Trucks</a>
Note: Did I mention that an XPath Visualizer will help you a lot?
For a robust XPath, which will work no matter how many tr/li elements are between the two text labels, try:
(//tr
[td[contains(., 'Refine by Vehicle Types')]]
)[1]
/following-sibling::tr[not(preceding-sibling::tr
[contains(., 'Refine by Category')])]
/td/div/table
/tr/td/font
/ul/li/a
(Borrowing from #Dimitre's formatting.)
The above is inefficient (could be O(n^2)), so if you have a long page, it could get slow.
But for moderate pages it should be fine.
I am using phpQuery to get the data from elements.
I'm trying to get the values from first td, seconds td and href link from each tr.
<table>
<tr class="A2">
<td> Text 1 </td>
<td> Text 2 </td>
<td> Text 3 </td>
<td> Text 131 </td>
</tr>
<tr class="A2">
<td> Text 4 </td>
<td> Text 5 </td>
<td> Text 6 </td>
<td> Text 123213 </td>
</tr>
<tr class="A2">
<td> Text 7 </td>
<td> Text 8 </td>
<td> Text 9 </td>
<td> Text 213213 </td>
</tr>
</table>
How to do this? I have tried:
<?
require('phpQuery.php');
$file = file_get_contents('test.txt', true);
$html = phpQuery::newDocument($file);
foreach($html->find('.A2') as $tag) {
echo pq('td'); // problem here?
}
?>
I guess you have them switched..
foreach(pq('.A2') as $tag) {
$tds = pq($tag)->find('td');
}
To get a value from each td, you can iterate over it inside:
foreach(pq('.A2') as $tag) {
foreach(pq($tag)->find('td') as $td) {
// stuff
}
}
pq() would return a list of matching nodes (your <td> tags, in this case). You have to iterate over that list:
foreach(pq('td') as $td) {
... do something ...
}