Jump to content

PHP/MYSQL including php, not echo


sp@rky13

Recommended Posts

Sorry about the title. What I need to do is this. Within my code I will need to have something similar to this in the code: "x BETWEEN 250 AND 299 AND y BETWEEN 240 AND 250". What I need though is to have a value similar to that associated with the numbers 1 to 99. So if a person enters 4 in the box ($k1) then I need to have a code like this: "x BETWEEN 250 AND 299 AND y BETWEEN 240 AND 250" included within my php code. But if that person had entered 99 it would then be "x BETWEEN 34 AND 99 AND y BETWEEN 1 AND 22". Note: The numbers I used are made up.

 

This was one way of doing it that I thought of but it failed :( :

$1 = "x BETWEEN 35 AND 40 AND y BETWEEN 70 AND 80";
$2 = "x BETWEEN 23 AND 67 AND y BETWEEN 15 AND 78";
$3 = "x BETWEEN 45 AND 67 AND y BETWEEN 14 AND 69";
$4 = "x BETWEEN 176 AND 999 AND y BETWEEN 1 AND 999";
/*..... All the way to 99*/

 

And then I'd have something like this:

if ($k1 =='1')
include $1;
if ($k1 =='2')
include $2;
if ($k1 =='3')
include $3;
if ($k1 =='4')
include $4;

 

But that failed to. Include would not work in this situation. I hope at the very least that that gives a clue to what I'm doing.

 

The mainm problem is that I need to put the "x BETWEEN 176 AND 999 AND y BETWEEN 1 AND 999" etc... into the actual php code like so:

 

<?php
$con = mysql_connect("localhost","","");
if (!$con)
{
   die('Could not connect: ' . mysql_error());
}

mysql_select_db("wwwspark_tribalwars", $con);
  
$result = mysql_query("SELECT GROUP_CONCAT(CONCAT_WS('|',b.x,b.y) SEPARATOR ' ') AS xy FROM players_en28 a INNER JOIN villages_en28 b ON a.id = b.player WHERE a.name = '$vpt_sub' AND [b]x BETWEEN 0 AND 999 AND y BETWEEN 0 AND 999[/b] GROUP BY a.id");

while($row = mysql_fetch_array($result))
{
   echo $row['xy']; 
}
mysql_close($con);
?>

 

Thank you,

Sp@rky13

Link to comment
https://forums.phpfreaks.com/topic/173509-phpmysql-including-php-not-echo/
Share on other sites

What I need to achieved is this:

 

I use this code (or similar):

 

<?php
$con = mysql_connect("localhost","","");
if (!$con)
{
   die('Could not connect: ' . mysql_error());
}

mysql_select_db("wwwspark_tribalwars", $con);
  
$result = mysql_query("SELECT GROUP_CONCAT(CONCAT_WS('|',b.x,b.y) SEPARATOR ' ') AS xy FROM players_en28 a INNER JOIN villages_en28 b ON a.id = b.player WHERE a.name = '$vpt_sub' AND x BETWEEN 0 AND 999 AND y BETWEEN 0 AND 999 GROUP BY a.id");

while($row = mysql_fetch_array($result))
{
   echo $row['xy']; 
}
mysql_close($con);
?>

 

This is the bit that needs changing:  "x BETWEEN 0 AND 999 AND y BETWEEN 0 AND 999". That piece of code needs to change depending on what number is entered in the box $k1. If the number 0 is entered then the code needs to be: "x BETWEEN 0 AND 99 AND y BETWEEN 0 AND 99" whereas if the number 1 is entered it would need to be: "x BETWEEN 100 AND 199 AND y BETWEEN 0 AND 99" and then for every number up to 99 where 99 would be: "x BETWEEN 900 AND 999 AND y BETWEEN 900 AND 999". If anyone can tell me how this could be done it would be helpful, and then I can put it all together.

 

Not sure if this helps or not, but this is currently what i'm doing which is very annoying:

I have an include setup of:

<?php if ($k1=="0")
include "tribe_28_k0.html";
elseif ($k1=="1")
include "tribe_28_k1.html";
elseif ($k1=="2")
include "tribe_28_k2.html";
elseif ($k1=="3")
include "tribe_28_k3.html";
elseif ($k1=="4")
include "tribe_28_k4.html";
elseif ($k1=="5")
include "tribe_28_k5.html";
elseif ($k1=="6")
include "tribe_28_k6.html";
elseif ($k1=="7")
include "tribe_28_k7.html";
elseif ($k1=="8")
include "tribe_28_k8.html";
elseif ($k1=="9")
include "tribe_28_k9.html";
elseif ($k1=="10")
include "tribe_28_k10.html";
elseif ($k1=="11")
include "tribe_28_k11.html";
elseif ($k1=="12")
include "tribe_28_k12.html";
elseif ($k1=="13")
include "tribe_28_k13.html";
elseif ($k1=="14")
include "tribe_28_k14.html";
elseif ($k1=="15")
include "tribe_28_k15.html";
elseif ($k1=="16")
include "tribe_28_k16.html";
elseif ($k1=="17")
include "tribe_28_k17.html";
elseif ($k1=="18")
include "tribe_28_k18.html";
elseif ($k1=="19")
include "tribe_28_k19.html";
elseif ($k1=="20")
include "tribe_28_k20.html";
elseif ($k1=="21")
include "tribe_28_k21.html";
elseif ($k1=="22")
include "tribe_28_k22.html";
elseif ($k1=="23")
include "tribe_28_k23.html";
elseif ($k1=="24")
include "tribe_28_k24.html";
elseif ($k1=="25")
include "tribe_28_k25.html";
elseif ($k1=="26")
include "tribe_28_k26.html";
elseif ($k1=="27")
include "tribe_28_k27.html";
elseif ($k1=="28")
include "tribe_28_k28.html";
elseif ($k1=="29")
include "tribe_28_k29.html";
elseif ($k1=="30")
include "tribe_28_k30.html";
elseif ($k1=="31")
include "tribe_28_k31.html";
elseif ($k1=="32")
include "tribe_28_k32.html";
elseif ($k1=="33")
include "tribe_28_k33.html";
elseif ($k1=="34")
include "tribe_28_k34.html";
elseif ($k1=="35")
include "tribe_28_k35.html";
elseif ($k1=="36")
include "tribe_28_k36.html";
elseif ($k1=="37")
include "tribe_28_k37.html";
elseif ($k1=="38")
include "tribe_28_k38.html";
elseif ($k1=="39")
include "tribe_28_k39.html";
elseif ($k1=="40")
include "tribe_28_k40.html";
elseif ($k1=="41")
include "tribe_28_k41.html";
elseif ($k1=="42")
include "tribe_28_k42.html";
elseif ($k1=="43")
include "tribe_28_k43.html";
elseif ($k1=="44")
include "tribe_28_k44.html";
elseif ($k1=="45")
include "tribe_28_k45.html";
elseif ($k1=="46")
include "tribe_28_k46.html";
elseif ($k1=="47")
include "tribe_28_k47.html";
elseif ($k1=="48")
include "tribe_28_k48.html";
elseif ($k1=="49")
include "tribe_28_k49.html";
elseif ($k1=="50")
include "tribe_28_k50.html";
elseif ($k1=="51")
include "tribe_28_k51.html";
elseif ($k1=="52")
include "tribe_28_k52.html";
elseif ($k1=="53")
include "tribe_28_k53.html";
elseif ($k1=="53")
include "tribe_28_k53.html";
elseif ($k1=="53")
include "tribe_28_k53.html";
elseif ($k1=="54")
include "tribe_28_k54.html";
elseif ($k1=="55")
include "tribe_28_k55.html";
elseif ($k1=="56")
include "tribe_28_k56.html";
elseif ($k1=="57")
include "tribe_28_k57.html";
elseif ($k1=="58")
include "tribe_28_k58.html";
elseif ($k1=="59")
include "tribe_28_k59.html";
elseif ($k1=="60")
include "tribe_28_k60.html";
elseif ($k1=="61")
include "tribe_28_k61.html";
elseif ($k1=="62")
include "tribe_28_k62.html";
elseif ($k1=="63")
include "tribe_28_k63.html";
elseif ($k1=="64")
include "tribe_28_k64.html";
elseif ($k1=="65")
include "tribe_28_k65.html";
elseif ($k1=="66")
include "tribe_28_k66.html";
elseif ($k1=="67")
include "tribe_28_k67.html";
elseif ($k1=="68")
include "tribe_28_k68.html";
elseif ($k1=="69")
include "tribe_28_k69.html";
elseif ($k1=="70")
include "tribe_28_k70.html";
elseif ($k1=="71")
include "tribe_28_k71.html";
elseif ($k1=="72")
include "tribe_28_k72.html";
elseif ($k1=="73")
include "tribe_28_k73.html";
elseif ($k1=="74")
include "tribe_28_k74.html";
elseif ($k1=="75")
include "tribe_28_k75.html";
elseif ($k1=="76")
include "tribe_28_k76.html";
elseif ($k1=="77")
include "tribe_28_k77.html";
elseif ($k1=="78")
include "tribe_28_k78.html";
elseif ($k1=="79")
include "tribe_28_k79.html";
elseif ($k1=="80")
include "tribe_28_k80.html";
elseif ($k1=="81")
include "tribe_28_k81.html";
elseif ($k1=="82")
include "tribe_28_k82.html";
elseif ($k1=="83")
include "tribe_28_k83.html";
elseif ($k1=="84")
include "tribe_28_k84.html";
elseif ($k1=="85")
include "tribe_28_k85.html";
elseif ($k1=="86")
include "tribe_28_k86.html";
elseif ($k1=="87")
include "tribe_28_k87.html";
elseif ($k1=="88")
include "tribe_28_k88.html";
elseif ($k1=="89")
include "tribe_28_k89.html";
elseif ($k1=="90")
include "tribe_28_k90.html";
elseif ($k1=="91")
include "tribe_28_k91.html";
elseif ($k1=="92")
include "tribe_28_k92.html";
elseif ($k1=="93")
include "tribe_28_k93.html";
elseif ($k1=="94")
include "tribe_28_k94.html";
elseif ($k1=="95")
include "tribe_28_k95.html";
elseif ($k1=="96")
include "tribe_28_k96.html";
elseif ($k1=="97")
include "tribe_28_k97.html";
elseif ($k1=="98")
include "tribe_28_k98.html";
elseif ($k1=="99")
include "tribe_28_k99.html";
else
echo'<script type="text/javascript">

alert ("You have entered an invalid continent. Please press ok and then the back button in your browser")

</script>';
?>

 

and then each document has something like this in it which is the full code:

 

<?php
$con = mysql_connect("localhost","","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

  mysql_select_db("wwwspark_tribalwars", $con);
  
$result = mysql_query("SELECT * FROM players_en28 WHERE name = '$vpt_sub'");

while($row = mysql_fetch_array($result))
  {
  $id = $row['id'];
  }

$result2 = mysql_query("SELECT * FROM villages_en28 WHERE player = '".$id."' AND x BETWEEN 0 AND 99 AND y BETWEEN 0 AND 99");

while($row2 = mysql_fetch_array($result2))
  {
  echo $row2['x']."|".$row2['y']." ";
  }
mysql_close($con);
?>

If the number 0 is entered then the code needs to be: "x BETWEEN 0 AND 99 AND y BETWEEN 0 AND 99" whereas if the number 1 is entered it would need to be: "x BETWEEN 100 AND 199 AND y BETWEEN 0 AND 99" and then for every number up to 99 where 99 would be: "x BETWEEN 900 AND 999 AND y BETWEEN 900 AND 999". If anyone can tell me how this could be done it would be helpful, and then I can put it all together.

 

According to that description there doesn't seem to be any pattern to how these numbers increment. If that is indeed that case you will need to do this manually, otherwise, your going to need to describe the pattern allot clearer.

 

Have you used loops before? Maybe you can try and figure it out yourself.

It's more that the x BETWEEN blah blah blah relates to the specific number of 0/1/2/blah blah blah. So if there's a way to like set id's or something within the code. But it's not to echo, as it is not seen, just part of the code. I'll look at loops now

 

EDIT: Looked at loops. That would work but how can i include that in the code. I need to echo but not echo to others but within the code

Looked at loops. That would work but how can i include that in the code. I need to echo but not echo to others but within the code

 

I know what you need to do, just don't understand the pattern.

 

You would use something like....

 

for ($i=0; $i<=99; $i++) {
  if ($k == $i) {
    $q = "x BETWEEN {$i} AND 99 AND y BETWEEN {$i} AND 99";
    break;
  }
}
$query = "SELECT * FROM villages_en28 WHERE player = '{$id}' AND {$q}";

 

Again, however, you need to be allot clearer with the pattern.

Slightly confused. A few things, the bolded bits in here are the things that change: "x BETWEEN 0 AND 99 AND y BETWEEN 0 AND 99" so not sure how the id thing goes in there, if at all????

 

Also, how would I do the one for 2. I'm slightly confused how you're doing it so that's why I ask

the bolded bits in here are the things that change: "x BETWEEN 0 AND 99 AND y BETWEEN 0 AND 99" so not sure how the id thing goes in there, if at all????

 

Yes, but doesn't your final query end up looking something like....

 

"SELECT * FROM villages_en28 WHERE player = '".$id."' AND x BETWEEN 0 AND 99 AND y BETWEEN 0 AND 99"

 

Thats what the above ends up looking like.

 

Also, how would I do the one for 2. I'm slightly confused how you're doing it so that's why I ask

 

I don't know. You still haven't described the pattern involved.

This is what it's like:

 

0 = "x BETWEEN 0 AND 99 AND y BETWEEN 0 AND 99"

1= "x BETWEEN 100 AND 199 AND y BETWEEN 0 AND 99"

2= "x BETWEEN 200 AND 299 AND y BETWEEN 0 AND 99"

3= "x BETWEEN 300 AND 399 AND y BETWEEN 0 AND 99"

4= "x BETWEEN 400 AND 499 AND y BETWEEN 0 AND 99"

5= "x BETWEEN 500 AND 599 AND y BETWEEN 0 AND 99"

6= "x BETWEEN 600 AND 699 AND y BETWEEN 0 AND 99"

7= "x BETWEEN 700 AND 799 AND y BETWEEN 0 AND 99"

8= "x BETWEEN 800 AND 899 AND y BETWEEN 0 AND 99"

9= "x BETWEEN 900 AND 999 AND y BETWEEN 0 AND 99

10= "x BETWEEN 0 AND 99 AND y BETWEEN 100 AND 199"

.....

20= "x BETWEEN 0 AND 99 AND y BETWEEN 200 AND 299"

.....

30= "x BETWEEN 0 AND 99 AND y BETWEEN 300 AND 399"

.....

40= "x BETWEEN 0 AND 99 AND y BETWEEN 400 AND 499"

.....

50= "x BETWEEN 0 AND 99 AND y BETWEEN 500 AND 599"

.....

60= "x BETWEEN 0 AND 99 AND y BETWEEN 600 AND 699"

.....

70= "x BETWEEN 0 AND 99 AND y BETWEEN 700 AND 799"

.....

80= "x BETWEEN 0 AND 99 AND y BETWEEN 800 AND 899"

.....

90= "x BETWEEN 0 AND 99 AND y BETWEEN 900 AND 999"

.....

99= "x BETWEEN 900 AND 999 AND y BETWEEN 900 AND 999"

 

That's the general idea. Notice the bolded bits and you'll see the pattern

Between 10 and 19 would be:

 

10 = "x BETWEEN 0 AND 99 AND y BETWEEN 100 AND 199"

11= "x BETWEEN 100 AND 199 AND y BETWEEN 100 AND 199"

12= "x BETWEEN 200 AND 299 AND y BETWEEN 100 AND 199"

13= "x BETWEEN 300 AND 399 AND y BETWEEN 100 AND 199"

14= "x BETWEEN 400 AND 499 AND y BETWEEN 100 AND 199"

15= "x BETWEEN 500 AND 599 AND y BETWEEN 100 AND 199"

16= "x BETWEEN 600 AND 699 AND y BETWEEN 100 AND 199"

17= "x BETWEEN 700 AND 799 AND y BETWEEN 100 AND 199"

18= "x BETWEEN 800 AND 899 AND y BETWEEN 100 AND 199"

19= "x BETWEEN 900 AND 999 AND y BETWEEN 100 AND 199

Hi

 

If there is a pattern then I would try some calculation.

 

This seems to cover your example:-

 

$yLow = floor($k1 / 10) * 100;
$yHigh = $yLow + 99;

$xLow = ($k1 % 10) * 100;
$xHigh = $yLow + 99;

$BetweenClause = "x BETWEEN $xLow AND $xHigh AND y BETWEEN $yLow AND $yHigh";

 

If there wasn't a pattern to it then I would just use 4 arrays, and use the $k1 as the subscript to get the values to build up the BETWEEN statement.

 

All the best

 

Keith

how would I use an array. I need it to be decided by what is entered in $k1. How could that be done?

 

EDIT: That patterns wouldn't work though this one would:

 

The first digit in the k (example 6 in 64) multiplied by 100 is the low value of y and then the high value is +99. Then for x, it's the second digit multiplied by 100 and then the high x is +99 to that

 

EDIT2: I can do this I think. I'll go try. Be back in a bit with any possible problems lol. Thank you both

 

EDIT3: Got a question, how can I look at just the first/last digit and how can I deal with having a 0/1/2/3/4/5/6/7/8/9 (1 digit number). What I need is for it to interpret that as a 00/01/02/etc...

how would I use an array. I need it to be decided by what is entered in $k1. How could that be done?

 

If you needed to, something like this.

 

$xlow = array(0,100,200,300,400,etc);
$xhigh = array(99,199,299,399,499,etc);
$ylow = array(0,0,0,0,0,etc);
$high = array(99,99,99,99,99,etc);

$BetweenClause = "x BETWEEN ".$xlow[$k1]." AND ".$xhigh[$k1]." AND y BETWEEN ".$ylow[$k1]." AND ".$yhigh[$k1];

 

The first digit in the k (example 6 in 64) multiplied by 100 is the low value of y and then the high value is +99. Then for x, it's the second digit multiplied by 100 and then the high x is +99 to that

 

Pretty much what I have done, except mathematically.

 

EDIT3: Got a question, how can I look at just the first/last digit and how can I deal with having a 0/1/2/3/4/5/6/7/8/9 (1 digit number). What I need is for it to interpret that as a 00/01/02/etc...

 

No need do. Divide by 10 and use floor to get the number of tens. Mod 10 to get the remainders.

 

All the best

 

Keith

Ok I am using your original idea of this:

 

$yLow = floor($k1 / 10) * 100;
$yHigh = $yLow + 99;

$xLow = ($k1 % 10) * 100;
$xHigh = $yLow + 99;

$BetweenClause = "x BETWEEN $xLow AND $xHigh AND y BETWEEN $yLow AND $yHigh";

 

The y works perfectly but not sure how I can get the x to work. The x is basically: the second digit divided by 100 for low and then that + 99 for high. Not sure how I can do that in php. Or is there another mathematical way to do it?

 

EDIT: Saw the thing about mod in you post. Never heard of it. Did a search and it's some maths term I've never heard of. If that's the way to do it can you explain/point me in the right direction

Hi

 

Mod gives you the remainder from a division. In php % gives you the mod. So 19 % 10 gives you 9. Think of mod as giving you (say) $x - (floor($x / 10) * 10)

 

The code I gave you uses mod to get that remainder and multiplies it by 100 to get the lower x figure. Then adds 99 to it to get the upper x figure.

 

All the best

 

Keith

There's the code as a test:

 

<?php 
$yLow = floor(64 / 10) * 100;
$yHigh = $yLow + 99;

$xLow = (64 % 10) * 100;
$xHigh = $yLow + 99;

$BetweenClause = "x BETWEEN $xLow AND $xHigh AND y BETWEEN $yLow AND $yHigh";
echo $BetweenClause;
?>

 

but the echo gives me this:

 

x BETWEEN 400 AND 699 AND y BETWEEN 600 AND 699

 

which is wrong??????????

 

EDIT: Found mistake. Had ylow instead of xlow

Ok it all works. Thank you very much. I have now put it in my code but it adds a line break. Why? Here's the code I have put it in:

 

<?php
$yLow = floor($k1 / 10) * 100;
$yHigh = $yLow + 99;

$xLow = ($k1 % 10) * 100;
$xHigh = $xLow + 99;
$BetweenClause = "x BETWEEN $xLow AND $xHigh AND y BETWEEN $yLow AND $yHigh";
?>

<?php
$con = mysql_connect("localhost","*******","******");
if (!$con)
{
   die('Could not connect: ' . mysql_error());
}

mysql_select_db("wwwspark_tribalwars", $con);

$result = mysql_query("SELECT c.player AS playerid, CONCAT_WS('|',c.x,c.y) AS xy FROM ally_en28 a INNER JOIN players_en28 b ON a.id = b.ally INNER JOIN villages_en28 c ON b.id = c.player WHERE a.tag = '$vpt_sub' AND $BetweenClause ORDER BY c.player") or die(mysql_error());

while($row = mysql_fetch_array($result))
{
echo $row['xy']." "; 
}
mysql_close($con);
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.