자동 마커 생성(3)

이명진·2022년 3월 28일
0

for문을 이용한 반복으로 마커를 생성하려고 하는데
php에서는 ""와 ''때문에 막히고
js에서는 php와의 연동에서 막힌다.

				<?php
				
				session_start();
				include"./inc/dbcon.php";
				
				$sql = "select * from restaurants;";
				
				$result = mysqli_query($dbcon,$sql);
				$row = mysqli_num_rows($result);
				echo $row;
				echo "<br>";
				
				
				$set = "set @rownum = 0;";
				mysqli_query($dbcon,$set);
				$result = mysqli_query($dbcon,$sql);
				echo "<br>";
				
				
				for($i = 1; $i <= $row; $i=$i+1){
					$sql = "select R.* from (select @rownum:=@rownum+1 as row, A.* from restaurants A where (@rownum:=0)=0) R where row = $i;";
				
					$result = mysqli_query($dbcon,$sql);
					$rows = mysqli_fetch_assoc($result);
					echo "<br>";
					echo $i;
					echo "<br>";
					echo "<br>";
				}
				
					?>


<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<title>Document</title>
	</head>
	<body>
		<script>
			var positions = [];

			
		</script>
	</body>

</html>
<script>
			var positions = [];
			var i = <?php echo $i ?>;
			for(i = 1; i <= <?php echo $row ?>; i++){
				<?php
					$set = "set @rownum = 0;";
					mysqli_query($dbcon,$set);
					$sql = "select R.* from (select @rownum:=@rownum+1 as row, A.* from restaurants A where (@rownum:=0)=0) R where row = $i;";
					
					$result = mysqli_query($dbcon,$sql);
					$rows = mysqli_fetch_assoc($result);
					echo $sql;
				?>;
				
				var marker =  {
						index: 1,
            title: <?php echo $rows["restaurantName"] ?>,
            latlng: new kakao.maps.LatLng(<?php echo $rows['location'] ?>),
            content: '<div class="overlaybox">' +
            '    <div class="boxtitle"><?php echo $rows["restaurantName"]?></div>' +
            '    <div class="first">' +
            '        <div class="triangle"></div>' +
            '        <div class="categori"><?php echo $rows["categori"]?></div>' +
            '    </div>' +
            '    <div class="instaId"><?php echo $rows["instaId"]?></div>' +
            '    <ul class="information">' +
            '        <li class="address">' +
            '            <div class="addr">주소</div>' +
            '            <div class="addr1"><?php echo $rows["addr1"]?></div>' +
            '            <div class="addr2"><?php echo $rows["addr2"]?></div>' +
            '        </li>' +
            '        <li class="review">' +
            '            <div class="review1">한줄평</div>' +
            '            <div class="review2"><?php echo $rows["review"]?></div>' +
            '        </li>' +
            '    </ul>' +
            '</div>'
          };


				// positions.push(marker);
				// console.log(positions);
			}
		</script>

계속 빈값 오류가 나길래 확인해보니 php의 $i가 정의되지 않아서 $sql값을 넣었을 때 DB에서 오류가 발생해서 $result값을 불러올 수 없었다.

$sql에 있는 $i를 정의하고 var i 값과 같게 만들어야한다.

1을 대입해봤을때 값을 잘 불러오는걸 보니 $i로 인한 오류가 맞았다.
그리고 kakao is not defined의 오류를 보고 js를 불러오는걸 까먹었다는걸 알았다.

<script
        type="text/javascript"
        src="//dapi.kakao.com/v2/maps/sdk.js?appkey=*************************"
      ></script>

for문 안에 while문을 넣어서 조건을 만족할 때까지 $i++을 하려했는데 $i값을 불러오는 순간 처음 정의한 $i = 1이 적용되어서 무한히 반복된다.

profile
프론트엔드 뉴비

0개의 댓글