简体   繁体   English

如何从数据库和 map 中获取数据进行反应?

[英]How to fetch data from database and map into react?

Data is coming from the database is perfectly fine But I don't know how to map that data or how to call that specific column that comes from the database.来自数据库的数据非常好但我不知道如何 map 该数据或如何调用来自数据库的特定列。 This is the output that console shows me, Data is coming这是控制台向我显示的 output,数据即将到来

Any suggestions, please.请有任何建议。 It would be better if anyone solves this with code.如果有人用代码解决这个问题会更好。

Rendering Function渲染 Function

  const [products,setProducts] = useState({});

  const classes = useStyles();

  useEffect(()=>{
    axios.post('/UserPortal/CartItems/checkout_details_summary.php' , {
        customer_id: localStorage.getItem('Id'),
    } )
    .then((response) => {
        console.log(response.data);
        setProducts(response.data);  
        

    })  
  },[])
 

Return返回

 return (
    
    <React.Fragment>
      <Typography variant="h4" gutterBottom>
        Order summary
      </Typography>
     
     
      <List disablePadding>
   
          <ListItem className={classes.listItem}  key={products.name} >
           <ListItemText secondary={'x'  + products.quantity   }>
                <Typography variant="h4" gutterBottom>
                  {products.name}  
               </Typography> 
             </ListItemText>  
              <Typography variant="h5"> {products.total} </Typography>
           </ListItem>
         
       
        <ListItem className={classes.listItem}>
          <ListItemText> <Typography variant="h4" gutterBottom>Total</Typography></ListItemText>
          <Typography variant="h5" className={classes.total}>
            {'Rs. '  + products.net_total  }
          </Typography>
        </ListItem>
   
      </List>
</React.Fragment>
);




Here is my PHP code这是我的 PHP 代码

<?php

require 'connect.php';

$postdata = file_get_contents("php://input");

if(isset($postdata) && !empty($postdata))
{
    $request = json_decode($postdata);
    
    $customer_id = $request->customer_id;

    $sql = ("SELECT * FROM `checkout_details` WHERE `customer_id` = '{$customer_id}' LIMIT 1");

    
$result = mysqli_query($con, $sql);
$row = mysqli_fetch_assoc($result);
echo $json = json_encode($row);

}

I think your const [products,setProducts] = useState({});我认为你的const [products,setProducts] = useState({}); should be an array of objects const [products,setProducts] = useState([]);应该是一个对象数组const [products,setProducts] = useState([]); . .

Then you can iterate over it with .map method.然后您可以使用.map方法对其进行迭代。 You can access the key of each Object destructuring params like this for example:您可以像这样访问每个 Object 解构参数的密钥,例如:

return (
    
    <React.Fragment>
      <Typography variant="h4" gutterBottom>
        Order summary
      </Typography>
     
     {products.map(({ name, total, net_total }) => 
      <List disablePadding>
   
          <ListItem className={classes.listItem}  key={products.name} >
           <ListItemText secondary={'x'  + products.quantity   }>
                <Typography variant="h4" gutterBottom>
                  {name}  
               </Typography> 
             </ListItemText>  
              <Typography variant="h5"> {total} </Typography>
           </ListItem>
         
       
        <ListItem className={classes.listItem}>
          <ListItemText> <Typography variant="h4" gutterBottom>Total</Typography></ListItemText>
          <Typography variant="h5" className={classes.total}>
            {'Rs. '  + net_total  }
          </Typography>
        </ListItem>
   
      </List>
    )}
</React.Fragment>
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM