I am using React Js as frontend and core Php as backend. I store data through a form in Mysql and then want to display that data in the UI on another page. There is no use of Props .Below is my React js code
import React, { useState, useEffect } from "react";
import { Container, Row, Col } from "react-bootstrap";
import axios from "axios";
function ItemList() {
const [items, setItems] = useState([]);
useEffect(() => {
axios.get("http://localhost/reactphp/items/").then(function (response) {
setItems(response.data);
// console.log(items);
});
}, []);
return (
<Container>
<Row>
{items.map((item, key) => (
<Col lg="4" sm="6" key={key}>
<img src={item.image} alt="pic" />
<h3>{item.name}</h3>
<span>{item.price}</span>
</Col>
))}
</Row>
</Container>
);
}
export default ItemList;
Below is my Php code.
<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: *");
header("Access-Control-Allow-Origin: *");
include 'conn.php';
$getData = "SELECT * FROM items";
$result = $conn->query($getData);
$data = array();
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$data[] = $row;
}
}
echo json_encode($data);
print_r($data);
?>
I tried it almost everywhere but could not get the answer