How to make php to print xml for this program? [duplicate] - php

This question already has answers here:
How to generate XML file dynamically using PHP?
(8 answers)
Closed 8 years ago.
friends i managed to make following output from a php code
0 0 1 0 2 0 0
0 0 0 0 0 0 3
1 0 0 1 0 2 0
0 0 1 0 0 0 3
2 0 0 0 0 0 0
0 0 2 0 0 0 0
0 3 0 3 0 0 0
1==>3 3==>4 4==>7 7==>2 3==>6 1==>5
<html>
<?php
// Start XML file, create parent node
$dom = new DOMDocument("1.0");
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node);
$result= array(array(0,0,1,0,2,0,0),array(0,0,0,0,0,0,3),array(1,0,0,1,0,2,0),
array(0,0,1,0,0,0,3),array(2,0,0,0,0,0,0),array(0,0,2,0,0,0,0),
array(0,3,0,3,0,0,0));
echo "<pre>";
for($k = 0; $k < 7; $k++){
for($j = 0; $j < 7; $j++){
echo $result[$k][$j],"\t";
}
echo "\n<br>";
}
$sum=0;
for($i = 0; $i < 7; $i++){
for($j = 0; $j < 7; $j++){
$sum += $result[$i][$j];
}
}
while( $sum > 0)
{
$i = 0;
while($i < 7){
$j=0;
while($j<7)
{
hm: if($result[$i][$j]!=0)
{ echo $i+1;
print "==>";
$result[$i][$j]=0;
$result[$j][$i]=0;
$i=$j;
$j=0;
echo $i+1;
print " ";
goto hm;
}
$j++;
}
$i++;
}
$sum=0;
for($i = 0; $i < 7; $i++){
for($j = 0; $j < 7; $j++){
$sum += $result[$i][$j];
}
}
}
//..................................................................
?>
</html>
EXPECTED OUTPUT
<markers>
<marker from="1" to="3"/>
<marker from="3" to="4"/>
<marker from="4" to="7"/>
<marker from="7" to="2"/>
<marker from="3" to="6"/>
<marker from="1" to="5"/>
</markers>

<?php
header('Content-type: text/xml');
/* create a dom document with encoding utf8 */
$domtree = new DOMDocument('1.0', 'UTF-8');
/* create the root element of the xml tree */
$xmlRoot = $domtree->createElement("xml");
/* append it to the document created */
$xmlRoot = $domtree->appendChild($xmlRoot);
$currentTrack = $domtree->createElement("track");
$currentTrack = $xmlRoot->appendChild($currentTrack);
$result= array(array(0,0,1,0,2,0,0),array(0,0,0,0,0,0,3),array(1,0,0,1,0,2,0),
array(0,0,1,0,0,0,3),array(2,0,0,0,0,0,0),array(0,0,2,0,0,0,0),
array(0,3,0,3,0,0,0));
/*echo "<pre>";
for($k = 0; $k < 7; $k++){
for($j = 0; $j < 7; $j++){
echo $result[$k][$j],"\t";
}
echo "\n<br>";
}*/
$sum=0;
for($i = 0; $i < 7; $i++){
for($j = 0; $j < 7; $j++){
$sum += $result[$i][$j];
}
}
while( $sum > 0)
{
$i = 0;
while($i < 7){
$j=0;
while($j<7)
{
hm: if($result[$i][$j]!=0)
{ /* you should enclose the following two lines in a cicle */
$currentTrack->appendChild($domtree->createElement('from',$i+1));
$result[$i][$j]=0;
$result[$j][$i]=0;
$i=$j;
/* you should enclose the following two lines in a cicle */
$currentTrack->appendChild($domtree->createElement('to',$j+1));
$j=0;
goto hm;
}
$j++;
}
$i++;
}
$sum=0;
for($i = 0; $i < 7; $i++){
for($j = 0; $j < 7; $j++){
$sum += $result[$i][$j];
}
}
}
/* get the xml printed */
echo $domtree->saveXML();
//..................................................................
?>
output
<xml>
<track>
<from>1</from>
<to>3</to>
<from>3</from>
<to>4</to>
<from>4</from>
<to>7</to>
<from>7</from>
<to>2</to>
<from>3</from>
<to>6</to>
<from>1</from>
<to>5</to>
</track>
</xml>

Related

Write a PHP code to print following number pattern

Write a PHP code to print following number pattern:
147
258
369
I am trying like this but its shows me shows below. how to convert column to row pattern.
<?php
$num = "";
for($i=1;$i<=9;$i++) {
$num .= $i;
if($i%3==0){
$num .="<br />";
}
}
echo $num;
?>
please help me
You need to use for loop inside for loop to achieve this. Below is the code
$num = "";
for( $i = 1; $i <= 3; $i++ ) {
for( $j = 0; $j <= 2; $j++) {
$k = $i + ( $j * 3 );
$num .= $k;
}
$num .= "<br />";
}
echo $num;
Here is another way to get this output.
for($i = 1; $i <= 3; $i++) {
$print = $i;
for($j = 1; $j <= 3; $j++) {
echo $print;
$print = $print + 3;
}
echo "<br />";
}

sort numbers in php,

hi i am using php to learn algorithms, i wanted to convert this psuedocode into php,
for i = 1 to n − 1
minval = A[i]
minindex = i
for j = i to n
if (A[j] < minval)
minval = A[j]
minindex = j
exchange A[i] and A[minindex]
this the corresponding code in php
$A = array(1, 4, 2, 3, 70, 10, 7 );
$n = sizeof($A);
for ($i = 0; $i == $n - 1; $i++){
for ($j = $i + 1; $j == $n; $j++){
if ($A[$i] > $A[$j]){
$temp = $A[$j];
$A[$j] = $A[$i];
$A[$i] = $temp;
}
}
}
print_r($A);
print_r is outputting the array as its original order, why my algorithms doents reorder the array ?
You should check your forloops :
for ($i = 0; $i == $n - 1; $i++){
for ($j = $i + 1; $j == $n; $j++){
should be
for ($i = 0; $i < $n - 1; $i++){
for ($j = $i + 1; $j < $n; $j++){
As the second argument in for is a requirement to continue the loop.

PHP Generate possible number with specific format

I need to generate possible number with some format/rule.
5 digit number
first digit > 0
for($x=0;$x=10;$x++) {
$first=1;$second=0;$third=0;$fourth=0;$five=0;
for($i=1;$i<10;$i++){
$first=$i; break;
}
for($i=0;$i<10;$i++){
$second=$i; break;
}
for($i=0;$i<10;$i++){
$third=$i; break;
}
for($i=0;$i<10;$i++){
$fourth=$i; break;
}
for($i=0;$i<10;$i++){
$five=$i; break;
}
echo $first.$second.$third.$fourth.$five.'\n';
}
desired result:
10000
10001
10002
...until
99999
but seems broken :(
Instead of using separate for loops you need to nest them inside one another:
for($i = 1; $i <= 9; $i++){
for($j = 0; $j <= 9; $j++){
for($k = 0; $k <= 9; $k++){
for($l = 0; $l <= 9; $l++){
for($m = 0; $m <= 9; $m++){
print("$i$j$k$l$m\n");
}
}
}
}
}
DEMO
But, obviously, a simpler approach would employ only one for loop:
for($i = 10000; $i <= 99999; $i++){
print("$i\n");
}
DEMO

PHP random DIV with css question

I tried to use PHP to make DIV random positions. we can look each 100px as a unit, type one small div is 1*1 ,type two small div is 1*2, type three small div is 2*1. All divs only allow show in a big div box 10*6. Here is my code.
<?php
function DIV1()
{
$choose1 = array("0","100","200","300","400","500","600","700","800","900");
$choose = array("0","100","200","300","400","500");
$rand_keys = array_rand($choose, 1);
$rand_keys1 = array_rand($choose1, 1);
echo "<div style=\"position:absolute;top:".$choose[$rand_keys]."px;left:".$choose1[$rand_keys1]."px;width:100px;height:100px;background:#ff0 none;\"></div>";
}
function DIV2()
{
$choose1 = array("0","100","200","300","400","500","600","700","800","900");
$choose = array("0","100","200","300","400");
$rand_keys = array_rand($choose, 1);
$rand_keys1 = array_rand($choose1, 1);
echo "<div style=\"position:absolute;top:".$choose[$rand_keys]."px;left:".$choose1[$rand_keys1]."px;width:100px;height:200px;background:#f00 none;\"></div>";
}
function DIV3()
{
$choose1 = array("0","100","200","300","400","500","600","700","800");
$choose = array("0","100","200","300","400","500");
$rand_keys = array_rand($choose, 1);
$rand_keys1 = array_rand($choose1, 1);
echo "<div style=\"position:absolute;top:".$choose[$rand_keys]."px;left:".$choose1[$rand_keys1]."px;width:200px;height:100px;background:#00f none;\"></div>";
}
echo '<div style="width:1000px;height:600px;">';
$sizes = array();
for($i = 0; $i < 15; $i ++) $sizes[] = DIV1($row);
for($i = 0; $i < 10; $i ++) $sizes[] = DIV2($row);
for($i = 0; $i < 5; $i ++) $sizes[] = DIV3($row);
shuffle($sizes);
for($i = 0; $i < 30; $i ++) echo $sizes[$i];
echo '</div>';
?>
I still have some css questions. in my code, I make postion:absolute and top left setting, but some divs will overlapping. how to solve it? Thanks.
1) You should use "return" instead of "echo" in the end line of 3 div functions so that array sizes can have any effect
2) Your problem is not standard, it is a little bit mathematical challenge, you must do "remembering" of positions and shuffling by hand
3) can this below code be your solution ? ( test here )
<?php
function divHtml($dtype, $x, $y) //$dtype: (1)=1x1yellow, (2)=1x2, (3)=2x1, (4)=1x1white
{ $ww =($dtype ==3) ?'200px':'100px'; $hh =($dtype==2)?'200px':'100px'; $bgcols =array('#ff0', '#f00', '#00f', '#fff');
$xx =($x*100) .'px'; $yy =($y*100) .'px'; $bgc =$bgcols[$dtype-1];
return "<div style='position:absolute; width:$ww;height:$hh; left:$xx;top:$yy; background:$bgc;'> $dtype </div>";
}
$divs =array(); //real divs array (html inside)
$cells =array(); //logical filled/notFilled (0/1) array
for ($i=0; $i<60; $i++) $cells[$i] =0;
function reserve($dtype, $x, $y)
{ global $cells, $divs; if ($y*10+$x >59 ||($cells[$y*10+$x])) return false;
switch ($dtype)
{ case 2: if ($y ==5 || $cells[$y*10+$x] ||$cells[($y+1)*10+$x]) return false; $cells[($y+1)*10+$x] =1; break;
case 3: if ($x ==9 || $cells[$y*10+$x] ||$cells[$y*10+$x+1]) return false; $cells[$y*10+$x+1] =1; break;
}
$cells[$y*10+$x] =1; $divs[] =divHtml($dtype, $x, $y); return true;
}
for($i = 0; $i < 10; $i ++) while (!reserve(2, rand(0,9), rand(0,5))) ; //adding 10 blocks of type2 (20cellsTotal)
for($i = 0; $i < 5; $i ++) while (!reserve(3, rand(0,9), rand(0,5))) ; //adding 5 blocks of type3 (10cellsTotal)
for($i = 0; $i < 15; $i ++) while (!reserve(1, rand(0,9), rand(0,5))) ; //adding 15 blocks of type1 (15cellsTotal)
for($i = 0; $i < 60; $i ++) if (!$cells[$i]) reserve(4, $i%10, floor($i/10)) ; //filling rest 15 cells with type4
//^go through all cells, but filling only 15
echo '<div style="position:absolute; width:1000px; height:600px;">';
foreach ($divs as $single) echo $single;
echo '</div>';
?>

how to get data from an array inside a recursive array?

Say I create a recursive array with this code:
$digits = 0;
$tens = 0;
$hundreds = 0;
for($i = 0; $i <= 100; $i++)
{
$myArray[$hundreds][$tens][$digits] = $i;
$digits++;
if($digits > 9)
{
$digits = 0;
$tens++;
}
if($tens > 9)
{
$tens = 0;
$hundreds++;
}
}
how could I echo out all the data fromt the 'tens array' == 2?
To be clear, I'd be looking for these results:
20 21 22 23 24 25 26 27 28 29
since im using base 10, I could just do this:
for($i=0; $i < 10; $i++)
{
echo $myArray[0][2][$i]
}
but what if i have no idea how many elements are in the digits array?
foreach($myArray[0][2] as $v) {
echo $v."<br>\n";
}
<?php
$tensArr = $myArray[0][2];
for($i= 0 ; $i < count($tensArr); $i++)
{
echo $tensArr[$i]."\n" ;
}

Categories