Can someone help me with a change to a php script that currently displays my information with echo print_r ($ results, true), I would like to make an html form that displays the information
enter image description here
I would like to make it look like this if possible
enter image description here
This is the full php page:
https://www.dropbox.com/s/ads312s19h8c0tc/license.php?dl=0
just replace your code
Line No : 296
echo '<textarea cols="100" rows="20">' . print_r($results, true) . '</textarea>';
new code is here
echo '<table border="1">';
foreach($result as $k=>$v){
echo '<tr><td>'. $k .'</td><td>'. $v .'</td></tr>';
}
echo "</table>";
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
}
}
while($row = mysql_fetch_array($result))
//Template for each card in search result
{
echo '<div class="sleeve">';
echo '<div class="card ', $row['name'], '">';
echo '<div class="front face">';
echo '<img src="/', $row['cardset'], '/', $row['name'], $row['altart'], '.jpg"', ' alt="', $row['name'], '" />';
echo '</div>';
echo '<div class="back face">';
echo '<a id="name">', $row['name'], '</a><br/>';
echo '<form name="info" action="">';
echo '<select name="set">';
//while???
echo '<option value="Zendikar">', $row['sets'],'</option>';
echo '</select>';
echo 'Foil:<input type="checkbox" name="foil" value="true"/><br/>';
echo '<select name="condition">';
echo '<option value="Near Mint">Mint</option>';
echo '<option value="Played">Played</option>';
echo '<option value="Damaged">Damaged</option>';
echo '</select>';
echo 'Trade:<input type="checkbox" name="trade" value="true"/ <br/>';
echo '</form>';
echo '<b>Rulings:</b> <br/>';
echo $row['rulings'];
echo '</div>';
echo '</div>';
echo '</div>';
}
//while??? It might be hard to see in that mess of echoes, but there's a section that I have no idea how to deal with. PHP is grabbing rows of info, and populating them nicely. It fills the screen with little boxes of info.
In this section, that I don't know how to deal with, the data in one of the rows contains multiple things (thing1, thing2, thing3) separated by a (, ) each time. I need each of those things in a new thing1
So I feel like there would be another while loop inside each card?
You're on the right track with a loop. Try something like this, which explodes the string into an array, based on the comma delimiter, with explode():
echo '<select name="set">';
foreach( explode( ',', $row['sets']) as $item)
echo '<option>', $item, '</option>';
echo '</select>';
You probably need a foreach statement there after exploding the String into an array:
instead of the //while line and the following one:
foreach (explode(',', $row['sets']) as $value)
echo '<option value="', $value, '">', $value,'</option>';
I guess you may actually have another value for each row (one to be displayed, the other one is the actual value you want to set), but then the String would look much more like "(key1=value1, key2=value2)" and then you need a little more work, but you get the idea.
Hope this helps.
Yes, you would need to first explode that row into an array
$list_of_things = explode(",", $row['whatever']);
and then use a while, or a foreach:
$thing_options = '';
foreach($list_of_things as $thing)
$thing_options .= "<option>$thing</option>";
You might also find the here document syntax useful:
print <<<TEMPLATE
<div class="sleeve">
<div class="card {$row['name']}">
<div class="front face">
<img src="/{$row['cardset']}/{$row['name']}{$row['altart']}.jpg"
alt="{$row['name']}" />
</div>
<div class="back face">
<a id="name">{$row['name']}</a>
<br/>
<form name="info" action="">
<select name="set">
{$thing_options}
<option value="Zendikar">{$row['sets']}</option>
</select>
...
TEMPLATE;
While all of the answers telling you to explode() the array are correct, I can't help but think that having a db column filled with comma separated values are a symptom that your database is not normalized. You should check out the following link for an introduction to db normalization: http://mikehillyer.com/articles/an-introduction-to-database-normalization/
I'd also recommend not echoing out HTML. Ideally, your PHP scripts should follow this pattern:
All PHP processing up front, including database queries and form handling. Results should be stored in variables.
|
|
|
V
Almost pure HTML template/view, with just enough display logic (if/else, loops, echo) to actually display the results you stored in the variables from step 1.
You'll find that debugging/editing is a lot simpler if you let PHP be the brains and HTML be the beauty. Just like how markup and styles should be separate, the same goes for scripting and display.
You can use PHP's explode-method to split the comma separated string into its values, and then handle that array in a foreach loop:
$raw = "one,two,three";
$values = explode("," $raw);
foreach($values as $value) {
echo $value;
}
I am creating a table (stats_1) dynamically and would like to have each row of different background color. So far, all the lines have the same background color.
I have the following php code that prints echoes out and statements:
$keys = array('Col1', 'Col2','Col3','Col4','Col5','Col6','Col7');
echo '<table id="stats_1"><tr>';
foreach ($keys as $column)
echo '<th>' . $column . '</th>';
echo '</tr>';
foreach ($data as $row){
echo '<tr class="alt">';
foreach ($keys as $column)
if (isset($row[$column])){
echo '<td>' . $row[$column];
} else {
echo '<td>' . '' . '</td>';
}
}
echo '</table>';
I need some help making EVERY OTHER ROW ($row) have a different COLOR, but don't know how to do that programmatically with the echo statement. So, it would alternate printing between:
echo '<tr class="alt">'; or echo '<tr>';
I define that in a class:
#stats_1 tr.alt td
{
color:#000000;
background-color:#E0E0FF;
}
THanks for your help/input.
Try:
$counter = 0;
foreach ($data as $row){
$counter++;
$class = $counter % 2 === 0 ? 'foo' : 'bar';
echo '<tr class="' . $class . '">';
// more code....
Where foo and bar are supposed to be the class names of your alternate colors.
CSS
http://www.w3.org/Style/Examples/007/evenodd - it cannot be done simply via CSS because support of nth-child property is not very good so far.
JavaScript
http://api.jquery.com/odd-selector/ - The task is quite easy with jQuery library in JavaScript. The disadvantage is that the colors may appear a little bit late and user may notice that (a disadvantage from MY perspective).
PHP
#Sarfraz presented a simple way how to accomplish the coloring with PHP.
Conclusion
PHP seems best bet until the CSS property nth-child is implemented in major browsers.
Need help with my simple PHP menu. I'd like to have something like that: when child elements < 6 display one column if > 6 display two columns. Any advices how to make it?
Regards
Here's the basic idea, you can adapt it to work with a table:
if (count($childs) < 6)
{
foreach ($childs as $child)
{
echo htmlspecialchars($child)."<br>";
}
}
else
{
for ($n=0;$n<count($childs);$n++)
{
echo htmlspecialchars($child)." ";
if ($n%2) echo "<br>";
}
}
For a table:
if (count($childs) < 6)
{
// Single row
echo "<tr>";
foreach ($childs as $child)
{
echo "<td>".htmlspecialchars($child)."</td>";
}
echo "</tr>";
}
else
{
// Multiple row
echo "<tr>";
for ($n=0;$n<count($childs);$n++)
{
echo "<td>".htmlspecialchars($child)."</td>";
if ($n%2) echo "</tr><tr>";
}
echo "</tr>";
}
There are other ways.
If sub menu contains more than 6 links I like submenu looks like:
Link
Link
Link
Link
If more than 6 links:
Link Link
Link Link
Link Link
I'm using WP, just need advice how to make it.