Im pulling in data about sports events via an xml feed, im using simplexml to do so. So far ive got a foreach loop that loops through all of the events and echos them out as a list of event names wrapped in <a> tags, pointing to a page event.php?=id (id is determined via the events attribute called id).
to do this im using
<?php
$xml = simplexml_load_file("openbet_cdn.xml");
foreach($xml->response->williamhill->class->type->market as $market) {
$market_attributes = $market->attributes();
printf("%s\n",
$market_attributes->id,
$market_attributes->name);
}
?>
the feed I'm using is http://whdn.williamhill.com/pricefeed/openbet_cdn?action=template&template=getHierarchyByMarketType&classId=5&marketSort=HH&filterBIR=N
What im having trouble with is on my page event.php i keep getting the first event in the xml feed displayed. To do this im using :
<?php
foreach ($xml->response->williamhill->class->type->market->participant as $participant) {
$participant_attributes = $participant->attributes();
echo "<tr>";
// EVENT NAME
echo "<td>";
echo "<a href=".$market_attributes['url'].">";
echo $participant_attributes['name'];//participants name
echo "</a>";
echo"</td>";
//ODDS
echo "<td>";
echo $participant_attributes['odds'];
echo "</td>";
echo "</tr>";
}
?>
I can see why it is because im not referencing the id which is in the URL of the event page. But I'm not quite sure of how to do this, any idea how I can tackle this ?
You just need to add an if within the loop so that you only target the event ID that matches the one in the querystring. A nested loop is also needed because you want to loop over each market to find the matching id, then loop over each of its participants.
foreach ($xml->response->williamhill->class->type->market as $market) {
if($market->attributes()->id == $_GET['id']) {
foreach($market->participant as $participant) {
$participant_attributes = $participant->attributes();
echo "<tr>";
// EVENT NAME
echo "<td>";
echo "<a href=".$market->attributes()->url.">";
echo $participant_attributes['name'];//participants name
echo "</a>";
echo"</td>";
//ODDS
echo "<td>";
echo $participant_attributes['odds'];
echo "</td>";
echo "</tr>";
}
break; // <-- we've found the target and echo'ed it so no need to keep looping
}
}
Related
I'm populating an html table with data from MySQL DB and I want to add a mailto function on the click of one of the columns. Problem is when I do it, the column is blank, but when I inspect it in the browser it shows up in the inspect panel.
My Code:
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>".$row['Property_ID']."</td>";
echo "<td>".$row['House_Number']."</td>";
echo "<td>".$row['Street_Address']."</td>";
echo "<td>".$row['Postal_Code']."</td>";
echo "<td>".$row['City']."</td>";
echo "<td>"."<a href='mailto:".$row['Submitted_By']."'></a>"."</td>";
echo "<td>".$row['Date_Submitted']."</td>";
echo "</tr>";
What the browser shows:
On inspection:
You <a> tag is empty, that's why you don't see your link.
You have to add your text inside <a> and </a>:
echo "<td>"
. "<a href='mailto:".$row['Submitted_By']."'>".$row['Submitted_By']."</a>"
. "</td>";
echo "<td>"."<a href='mailto:".$row['Submitted_By']."'>".$row['Submitted_By']."</a>"."</td>";
this line should be exactly like this.
I have an old PHP4 web app in which most of the pages looks like this(some pages has a left menu, some doesn't have a footer):
<?php
echo "<html>";
echo "<head><title>TITLE GOES HERE</title></head";
echo "<body>";
echo "<h2>THIS IS A TITLE</h2>";
// Here i fetch data from DB
echo "<table>";
echo "<tr>";
echo "</tr>";
foreach($rowsFromDB as $row) {
echo "<tr>";
// here i echo some <td> containing $row data
echo "</tr>";
}
echo "</table>";
echo "</body>";
echo "</html>";
?>
This is a simple example, the real ones contains a lot of spaghetti code (i'm italian, i like spaghetti but not in my code) and i'm trying to refactor/redesign it in some way. Rewrite the entire app from scratch (maybe with an MVC framework) is not an option because the app contains a lot of business logic i would like to keep.
My idea (for now) is to wrap the echos inside a renderer class, something like this:
<?php
class PageRenderer {
public static function renderHeader() {
echo "<html>";
echo "<head><title>TITLE GOES HERE</title></head";
echo "<body>";
echo "<h2>THIS IS A TITLE</h2>";
}
public static function renderContent($rowsFromDB) {
echo "<table>";
echo "<tr>";
echo "</tr>";
foreach($rowsFromDB as $row) {
echo "<tr>";
// here i echo some <td> containing $row data
echo "</tr>";
}
echo "</table>";
}
public static function renderFooter() {
echo "</body>";
echo "</html>";
}
}
$renderer=new PageRenderer();
$renderer->renderHeader();
// Fetch data from DB
$renderer->renderResults($rowsFromDB);
$renderer->renderFooter();
?>
The problem with the above solution is that is difficult to extend and maintain. Do you know any design pattern or any technique i could use for a better refactoring/redesign?
Thanks in advice and sorry for my bad english
I'd add a method, maybe call it renderColumn($tdParams = array()) which only has simple job of returning a single td element (as a string):
Initialize an empty string, $td_cell
Append to $td_cell an opening <td> tag, maybe accept an array of attributes and values for said td tag as paramater $tdParams which has been set a default value of an empty array.
Append to $td_cell a closing </td> tag.
return $td_cell
For rendering out your DB Rows, you may (at later point in time) have a query that has more, or less data points - thus will result in needing more or less td cells.
For your renderHeader method, I would add at least 2 parameters: title and maybe for the <h2> as you specified, as I can see that changing frequently.
The below code creates a table from mysql on a web page and changes the column background colour based on the gender column value being male or female but I want to extend this to change the row's colour without using javascript/jquery. Please can someone help me with this? :)
while($tablev2=mysql_fetch_assoc($records)) {
echo "<td>".$tablev2['primary_key']."</td>";
echo "<td>".$tablev2['name']."</td>";
if($tablev2['gender']=='male')
echo "<td style='background-color:powderblue;'>".$tablev2['gender']."</td>";
elseif($tablev2['gender']=='female')
echo "<td style='background-color:pink;'>".$tablev2['gender']."</td>";
else echo "<td>".$tablev2['gender']."</td>";
echo "<td><a target = '_blank' href='".$tablev2['link']."'>Click to see their facebook profile</a></td>";
echo "</tr>";
}
You just need to apply a style on the 'tr' tag depending on the gender value.
if($tablev2['gender']=='male'){
echo '<tr class="bg-male">';
}elseif($tablev2['gender']=='female'){
echo '<tr class="bg-female">';
}
while($tablev2=mysql_fetch_assoc($records)) {
if($tablev2['gender']=='male')
echo "<tr style='background-color:powderblue;'>";
elseif($tablev2['gender']=='female')
echo "<tr style='background-color:pink;'>";
else
echo "<tr>";
echo "<td>".$tablev2['primary_key']."</td>";
echo "<td>".$tablev2['name']."</td>";
echo "<td>".$tablev2['gender']."</td>";
echo "<td><a target = '_blank' href='".$tablev2['link']."'>Click to see their facebook profile</a></td>";
echo "</tr>";}
use background color to TR tag instead of TD tag.
otherwise you have to apply for all TD and it's a bad practice.
I've got a data feed I'm importing that has a load of 'markets', I want to have a main page displaying all the markets, so for that id use a foreach loop to go through the data and on each market make a listing.
Each market has a bunch of attributes as well as nested participants, I want to then make a page for each market, that displays some information about each participant.
So the user would navigate to index.php > event.php?id101
This is the bit were ive become stuck, how can i send the user to the right page, I was thinking of using
<?php
$xml = simplexml_load_file('f1_feed.xml');
foreach($xml->response->williamhill->class->type->market as $event) {
$event_attributes = $event->attributes();
echo "<tr>";
// EVENT NAME WRAPPED IN LINK TO EVENT
echo "<td>";
echo '<a href="event.php?id=' . $market_id . '">';
echo $event_attributes['name'];
echo "</a>";
echo"</td>";
// DATE
echo "<td>";
echo $event_attributes['date'];
echo "</td>";
echo "</tr>";
}
?>
but how can I set a var $market_id (from the xml feed) to add to the end of the url, so it sends me to the right page ?
(f1_feed.xml is the same as the live xml feed, its just local for development)
the feed I'm using is http://whdn.williamhill.com/pricefeed/openbet_cdn?action=template&template=getHierarchyByMarketType&classId=5&marketSort=HH&filterBIR=N
which im bring in using simplexml
This worked for me;
$xml = simplexml_load_file("openbet_cdn.xml");
foreach($xml->response->williamhill->class->type->market as $market) {
// that gives an object (native)
$market_attributes = $market->attributes();
printf("%s\n",
$market_attributes->id,
$market_attributes->name);
// that gives an array (useless way!)
// $market_attributes = (array) $market->attributes();
// printf("%s\n",
// $market_attributes['#attributes']['id'],
// $market_attributes['#attributes']['name']);
}
I am trying to create a hyperlink from two pieces of text split over two cells in a table row.
I am generating my table using PHP to echo out the results from my database to a table.
When it echo's it generates a hyperlink with GET variables at the end which allow the user to visit a page relevant to that information.
The problem is that I can't seem to generate a hyperlink that will go across those table cells, I have looked around the web and there is nothing that says I cannot do this.
As you can see from the screenshot below I am generating a hyperlink inside one table cell but I want the other table cell to have the same hyperlink.
Code
while ($row = $db->fetch_assoc($newest))
{
echo "<tr>";
echo "<td>";
echo "<a href='manager.php?method=view&id=".$row['id']."'>".$row['first_name']." ". $row['second_name']. "</td><td>".$row['company_name']."</a>";
echo "</td>";
echo "</tr>";
}
I have a feeling that I will just have to generate two separate hyperlinks for the table cells.
However I am hoping someone here can prove me wrong and save me a few lines of code.
Thanks :)
Using native hyperlinks, you will have to create separate wrappers for each cell.
However, if you want to use JS for linking and redirecting, you could do something like:
.....
<tr class="clickable" data-href="http://google.com">
<td>cell-1</td>
<td>cell-2</td>
<td>cell-3</td>
</tr>
....
and then:
$(function(){
$('tr.clickable').click(function(){
window.location.href = $(this).attr('data-href');
});
});
Simply work around it with JS:
echo "<tr onclick=\"location.href='manager.php?method=view&id=".$info.";'\">";
If you do not want to break the table structure (ie. putting the name and the company into one (multi-column) cell), there is IMHO no way other than generating two hyperlinks.
What you might want to do is to use some CSS for a hover effect and some JavaScript to register a user clicked on a cell (which you can, given the structure above, associate with the tr element).
You can not do it like this. Try instead:
while ($row = $db->fetch_assoc($newest))
{
$url = "manager.php?method=view&id=".$row['id'];
echo "<tr>";
echo "<td>" . $row['first_name']." ". $row['second_name']. "</td>";
echo "<td>" . $row['company_name'] . "</td>";
echo "</tr>";
}
while ($row = $db->fetch_assoc($newest))
{
echo "<tr>";
echo "<td>";
echo "<a href='manager.php?method=view&id=".$row['id']."'>".$row['first_name']." ". $row['second_name']."</a></td><a href='manager.php?method=view&id=".$row['id']."'>".$row['company_name']."</a><td></td>";
echo "</td>";
echo "</tr>";
}