Trying my first simplexml loop
I have got to this:
foreach ($xml->GetCapabilityResponse->Srvs->Srv as $rows)
{
echo $xml->GetCapabilityResponse->Srvs->Srv->GlobalProductCode . " ";
echo $xml->GetCapabilityResponse->Srvs->Srv->MrkSrv->LocalProductCode . " " ;
echo $xml->GetCapabilityResponse->Srvs->Srv->MrkSrv->ProductShortName . "<br>";
}
It returns the correct amount of rows '6' but all 6 are the same data from the first row, how do I adjust to show the 6 different rows own data, it's not limited to 6, could be 12 or more etc
thanks
Mazz
so if $rows is an array, you should rely on the item you are processing
$row->GlobalProductCode . " ";
Related
I want to create an array with the help of MYSQL and display it the following way:
while ($array = mysqli_fetch_assoc($res))
{
echo $array["first_name"] . ", "
. $array["last_name"] . ", "
. $array["personal_id"] . ", "
. $array["salary"] . ", "
. $array["birthday"] . "<br />";
}
To improve usability, I only want to display 10 results, put a link to the next 10 results at the bottom and display these on another page.
How do I have to change the while-condition in order to only display the first 10 results of my result?
I tried searching for a solution but I couldn't find anything that fits my needs, so any help is appreciated!
Thanks in advance!
Check offset and limit conditions in mysql.
Example: to show 16 - 26 records from result query.
$sql = "SELECT * FROM Table LIMIT 10 OFFSET 15"
If what you want is just a way to break out of the loop after the first 10 results.
$counter = 1;
while (($array = mysqli_fetch_assoc($res)) || ($counter < 10))
{
echo $array["first_name"] . ", "
. $array["last_name"] . ", "
. $array["personal_id"] . ", "
. $array["salary"] . ", "
. $array["birthday"] . "<br />";
$counter++;
}
I have seven columns representing days of the week in a user table 'team' storing the indices from nine daily ranges from which the user selects. Table timerange range uses the index rangeID. The user chooses from a multiple select list one or more timeranges and the indices are stored for that day as in comma delimited list:
+------+-------+---------+
|sun + mon + tue +
|-------+-------+---------+
|2,4,5 + 1,3 + 9 +
+------+-------+---------+
My challenge is to return a div table of the same presentation of that used to select the date ranges but with the selected options highlighted (selected) so that the user's availability can be displayed and or edited in the form.
I have attempted to get that result with the following query for one day, Sunday, but am failing with the result that while the selected options display correctly, each of the choices not selected displays twice in the select lst:
echo "<select multiple=\"multiple\" size=\"10\" name=\"sun[]\">";
echo "<option size=\"30\"></option>";
$sql = "SELECT sun, rangeID, timeranges FROM test, timerange ORDER BY rangeID";
foreach ($myconnect->query($sql) as $row) {
$index=explode(',', $row['sun']);
foreach($index as $sun) {
if ($row['rangeID']==$sun) {
echo "<option selected=\"selected>\" value='" . $row['rangeID'] . "'>" . $row['timeranges'] . "</option>";
} else {
echo "<option value='" . $row['rangeID'] . "'>" . $row['timeranges'] . "</option>";
}
}
}
echo "</select>";
Somewhere logic is failing me so I'd like help in correcting the code so that only the nine time ranges are displayed with zero, one or more options selected.
After a week of all sorts of gymnastics with code, adding to my gray hairs, I arrived at a solution that is not elegant but is fine since the action is to be called by one user (at a time) to pre-populate a form. It beats creating multiple tables, which will have many empty rows, to store 0-9 indexes of nine indexes. I'll definitely work on a more efficient table structure for storing the user-selected data and querying one or multiple users at a time.
$sql = "SELECT sun FROM test";
foreach ($myconnect->query($sql) as $days)
{
$index = explode(',', $days['sun']);
}
echo "<select multiple=\"multiple\" size=\"9\" name=\"sun[]\">";
$sql = "SELECT rangeID, timeranges FROM timerange ORDER BY rangeID";
foreach ($myconnect->query($sql) as $row)
{
$arr = array_merge($index, $row);
if ($row['rangeID'] == # $arr[0] or # $arr['rangeID'] == # $arr[1] or $row['rangeID'] ==
# $arr[2] or $row['rangeID'] == # $arr[3] or $row['rangeID'] == # $arr[4] or $row[
'rangeID'] == # $arr[5] or $row['rangeID'] == # $arr[5])
{
$selected = "selected=\"selected\"";
}
else
{
$selected = "";
}
echo "<option value=" . $row['rangeID'] . " " . $selected . ">" . $row['timeranges'] .
"</option>";
}
echo "</select>";
I am working on a "boxing records" database for a school project. The loop retrieves records from a SQL statement. I want to add a "VS" string of text between every other two lines in order to show records outputted somewhat like this.
Upcoming Fights
Sergey Kovalev (28-0-25)
VS
Jean Pascal (30-3-17)
Another Boxer (123-0-5)
VS
Some Boxer (123-3-1)
However, my current loop outputs like this
Sergey Kovalev (28-0-25)
VS
Jean Pascal (30-3-17)
VS
Another Boxer (123-3-1)
VS
Some Other Boxer (123-3-1)
VS
The loop I currently have is the following
foreach($records as $record) {
$i = 0;
echo $record['name'] . " (" . $record['wins'] . "-" . $record['losses'] . "-" . $record['kos'] . ")" . "<br>";
$i=$i*2;
if($i%2 == 0)
{
echo "VS <br/>";
}
else{
echo "<br />";
}
I know I could probably change the SQL in order to display two fighters in the same row, and then append "vs" on the echo, but I thought that just modifying the for loop would work by using a variable counter $i. I thought it would be pretty easy to make the "VS" appear between every two rows but im missing something in my logic.
You need to increment value of $i by one rather than multiplying it by 2 and initialize $i outside foreach loop.
$i = 0; // Initialize counter here
foreach($records as $record) {
echo $record['name'] . " (" . $record['wins'] . "-" . $record['losses'] . "-" . $record['kos'] . ")" . "<br>";
if($i%2 == 0)
{
echo "VS <br/>";
}
else
{
echo "<br />";
}
$i++; // Increment counter here
}
I am doing a profile quiz. This is just an example of code. Ultimately I will do a loop for all possible combinations. Basically, if the student chooses answer 3 to question 1 (what is your height - small, medium, large, xl, xxl) I want their pgheight to be stored as 4, sgheight as 9, cheight as 4, etc... My final array will consist of 16 $pos and 16 $attr with 5 scores for each combination. Hope this makes sense. Thanks for any help.
e.g.
function height(){
$pos=array("pg","sg","c");
$attr=array("height","weight","strength");
$pgheight=array(1,2,3,4,5);
$sgheight=array(6,7,8,9,10);
$cheight=array(1,2,3,4,5);
$ans=3;
$i=0;
// THIS IS THE CODE THAT DOESN'T SEEM TO WORK.
// My logic is $pgheight=$pgheight[3];
$pos[$i].$attr[$i]=$pos[$i].$attr[$i]."[".$ans."]";
echo $pgheight;
}
you either want to eval the statement or use two dollar signs. you're reusing the variable pgheight so its a bit confusing. let me give you an example instead.
$fruit = array("apples", "oranges", "pears");
$drinks = array("beer", "cider", "wine");
$krislikes = "drinks";
$whichone = 0;
echo "Kris likes " . ${$krislikes}[$whichone] . "<br>";
// or
$answer = $$krislikes;
echo "Kris likes " . $answer[$whichone] . "<br>";
// or
eval('$the_answer=$' . $krislikes . '[' . $whichone . '];');
echo "Kris likes " . $the_answer . "<br>";
hope that helps.
I am learning PHP and have been agonizing over how to write my code properly. I have a textarea that accepts user input which is several lines of coordinates (PT# Northing Easting) separated by spaces.
I have the form pass the textarea input to a php script to handle it.
I can get a foreach loop to do exactly what I want but only local to the foreach loop, the variables do not get passed to the outside of the loop.
I know this is a global variable scope issue, for the life of me I can't resolve this...
Below is my PHP code, I left out the HTML form I have no issues with it and know the textarea is being passed properly.
**Sample data that I am inputing:
1 728258.24000 774337.29700
2 728232.15735 774277.54650
3 728326.39614 774216.82428**
<?php
$i = 0;
$j = 0;
//The code below explodes my textarea into a string array
//of separated lines.
$textArea = explode("\r", $_POST['textArea']);
$textNum = array();
//The code below works internally, but the values remain here
//I wanted to get them to the variables below so I can do work
//to them.
foreach ($textArea as $textRows) {
//The code below explode the lines into elements separated by a space
$textRow = explode(" ", $textRows);
foreach ($textRow as $textItem) {
$textNum[i][j] = $textItem;
//The code below works within a local context
echo "(" . $i . " " . $j . ")" . $textNum[i][j] . "</br>";
$j++;
}
$i++;
$j = 0;
}
//The code below is not receiving values from the foreach loop
//I know this has something to do with the variable scope
//I must be way off in my approach any help would be appreciated!
echo "</br>";
echo "</br> 0 0 " . $textNum[0][0];
echo "</br> 0 1 " . $textNum[0][1];
echo "</br> 0 2 " . $textNum[0][2];
echo "</br> 1 0 " . $textNum[1][0];
echo "</br> 1 1 " . $textNum[1][1];
echo "</br> 1 2 " . $textNum[1][2];
echo "</br> 2 0 " . $textNum[2][0];
echo "</br> 2 1 " . $textNum[2][1];
echo "</br> 2 2 " . $textNum[2][2];
echo "</br> 3 0 " . $textNum[3][0];
echo "</br> 3 1 " . $textNum[3][1];
echo "</br> 3 2 " . $textNum[3][2];
?>
I hope I have explained this well enough, and appreciate any help I can get! Thanks!
There is an error in your second loop:
$textNum[i][j] = $textItem;
have to be:
$textNum[$i][$j] = $textItem;
You, need to prepend $ before your variables, i.e $textNum[$i][$j] instead of $textNum[i][j], also, it's best to initialize $i and $j and set them to 0 before the foreach loop.